lundi 6 août 2018

Context of nested functions inside object literal? [duplicate]

This question already has an answer here:

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 {}

enter image description here

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