Function defined with 'this', but executing without 'this'

后端 未结 3 410
滥情空心
滥情空心 2020-12-19 16:07

I was expecting the 2nd call of the \"taco\" function to generate a runtime error since I am not calling it with the \"this\" keyword:

function foo() {
    v         


        
3条回答
  •  一生所求
    2020-12-19 16:40

    foo(); // equals window.foo() , `this` equals `window` and `this.taco` equals `window.taco` and `window.taco`  equals `taco` as it is global
    
    new foo(); //creates a new object. this will give error because here `this.taco` is not `taco`
    

提交回复
热议问题