This question already has an answer here:
- How does the “this” keyword work? 20 answers
var obj ={ function1: function(){
console.log('function1', this);
return function function2(){
console.log('function2', this);
return function function3(){
console.log('function3', this);
};
};
};};
var f1 = new function1();//function1 function1 {}
var f2 = new f1();//function2 function2 {}
var f3 = f2();//function3 Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, frames: Window, …}
f3 = new f2();//function3 function3 {}
Why is it that in function3 'this' is bound to 'Window' object when invoked without 'new' prefix. Shouldn't 'this' be bound to the object 'obj' which owns it?
Aucun commentaire:
Enregistrer un commentaire