Why is this variable used in the function?
-
DD, help me plz !, though I don't understand why var self = this is used, as well as this is generally assigned, I read that it is used to use the main context of the function in nested functions, but for some reason in a simple example
function a() { let self = this; // widnow console.log(this); var fn = function () { console.log(this); //widnow console.log(self); // function a }; fn(); } a();
but it turns out that all console.log references the window objectJavaScript Kira Bentley, Feb 16, 2020 -
Wrong example (
function a() {
let self = this; // widnow
console.log(this);
var fn = function () {
console.log(this); //widnow
console.log(self); // function a - widnow
};
fn();
}
a();Anonymous
2 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!