1.在全局里写的 this===>window
当函数不作为对象的属性被调用时,也就是我们常说的普通函数方式,此时的 this 总是指
向全局对象。在浏览器的 JavaScript 里,这个全局对象是 window 对象
var name = 'xiaohao'
var getName = function(){
return this.name
}
console.log(getName());//输出xiaohao
2.普通函数,谁触发 this 就是谁
var name = 'aaa'
var obj ={
name:'xiaohao',
say:function(){
console.log(this.name);//xiaohao
}
}
obj.say()
3.如果是事件处理函数 this === >触发事件的元素
谁直接调用产生this的函数,this就指向谁。但是在使用了new操作符以后,构造函数内部的this指向的是新对象,也就是指向new出来的新实例。
var MyClass = function () {
this.name = 'xiaohao';
};
var obj = new MyClass();
console.log((obj.name)); // 输出:xiaohao
4.扩展--改变this指向的3个方法
如果是call apply bind this === 三个函数的实参一