1.方法调用模式中

sayName函数作为对象obj的方法调用,所以函数体中的this就代表obj对象。
2.函数调用模式

在这种函数调用的情况下 this 就是window ,其实也可以这样理解
sayHello ()的调用其实就是 window.sayHello();这样就好理解了。
3.构造函数模式

如果在一个函数前面增加new 关键词则意味着是创建一个由构造函数(Person)生成的对象 person , 构造函数中的this则属于由构造函数生成的那个对象 可以理解为 person = Person 中的this .
4.apply模式

当函数调用的时候,this代表的是window
当用apply模式调用sayName,并给它传入的第一个参数为person时,this被绑定到person对象上。apply的作用就是改变this指向
网友评论