第一种:函数执行模式
function add(a,b){
console.log(this);
return a+b;
}
add();//this===window//true
第二种:对象方法的调用模式
function Cat(){
this.show = function(){
console.log(this);
}
}
var c = new Cat();
c.show();//对象调用自己的方法
//this---c
//调用事件响应的方法都是,对象方法调用模式
第三种:构造器的调用
function Cat(){
this.show = function(){
console.log(this);
}
var c = new Cat();
//构造器调用模式,this指向 构造出来的对象
}
}
第四种:this—c//call 和 apply调用模式
function add(a,b){
this.result-a+b;
}
var p = {};//定义一个空对象
add.call(p,3,4);//在这个方法调用的时候,this指向了p
console.log(p.result);
//apply和call是一样的用法,只不过apply第二个参数用数组进行传递