前言java
在JavaScript中this的指向老是让人很困惑,它到底指的啥?为了完全弄清它,咱们就来探讨一下吧。this在不一样的执行环境,不一样的用法下会有所不一样,如下分几种状况,讨论this的指向。node
this在全局做用域中浏览器
// global scope
foo = 'abc';
alert(foo); // abc
this.foo = 'def';
alert(foo); // def
复制代码
在全局做用域/全局环境(global scope| global context)中,this指向的就是全局变量bash
this在函数(function)里ide
var boat = {
size: 'normal',
boatInfo: function() {
alert(this === boat);
alert(this.size);
}
};
boat.boatInfo(); // true, 'normal'
var bigBoat = {
size: 'big'
};
bigBoat.boatInfo = boat.boatInfo;
bigBoat.boatInfo(); // false, 'big'
复制代码
上面这段代码里,this指向谁?能够看到,boat对象有一个size属性,和一个boatInfo()方法。在boatInfo()方法里,alert出this是否和boat相等,以及this所指向的size属性。函数
当咱们去用boat.boatInfo()时,能够看到,this和boat是相等的,this.size的值就是boat.size的值nomaloop
当咱们建立一个新的对象,bigBoat,它的size属性为big,可是bigBoat这个对象没有boatInfo()方法,因而咱们把boat.boatInfo()的方法赋给了它。而后咱们调用bigBoat.boatInfo(),发现this不等于boat,this.size的值为bigui
this的指向改变了!this
The first thing you must realise is that the value of this inside any function is never static, it is always determined every time you call a function, but before the function actually executes it’s code. The value of this inside a function is actually provided by the parent scope in which the function was called, and more importantly, how the actual function syntax was written.spa
要理解以上的变化,首先要明白,在任何函数中,this的指向都不是静态的(static)。它老是在你调用一个函数,但还没有执行函数内部代码前被指定。(查看参考连接中的执行环境的文章,这个阶段,实际就是初始化变量对象,在初始化变量对象的时候,肯定了this的指向)实际上,this是 被调用的函数的父做用域 提供的,更重要的是,咱们要看看函数调用时是怎么写的。
当一个函数被调用时,应该立马看()左边的部分。
function bar() {
alert(this);
}
bar(); // global - because the method bar() belongs to the global object when invoked
// 这里,this指向的是全局对象。咱们先看()的左边,是bar,
// 那么bar属于谁呢?bar属于全局对象,因此this指向的就是全局对象。
var foo = {
baz: function() {
alert(this);
}
}
foo.baz(); // foo - because the method baz() belongs to the object foo when invoked
// 这里,this指向的是foo,先看()左边是baz,baz属于foo,因此baz里的this指向的就是foo
复制代码
若是代码都那么简单,那么this的指向也就简单明了了。来点复杂点的看看:
var foo = {
baz: function() {
alert(this);
}
}
foo.baz(); // foo - because baz belongs to the foo object when invoked
var anotherBaz = foo.baz;
anotherBaz(); // global - because the method anotherBaz() belongs to the global object when invoked, NOT foo
// this指向全局对象,()左边是anotherBaz,属于全局对象
能够看到baz()中this的指向总是变来变去的。再来看看嵌套在对象里的this的指向
var anum = 0;
var foo = {
anum: 10,
baz: {
anum: 20,
bar: function() {
console.log(this.anum);
}
}
}
foo.baz.bar(); // 20 - because left side of () is bar, which belongs to baz object when invoked
// ()左边是bar,bar属于foo.baz,因此this就是foo.baz,this.anum = foo.baz.anum = 20
var hello = foo.baz.bar;
hello(); // 0 - because left side of () is hello, which belongs to global object when invoked
// ()左边是hello,hello属于全局对象,因此this指向全局对象,this.anum = window.anum = 0
复制代码
再来看个例子:
const obj = {
name: 'spike',
friends: ['deer', 'cat'],
loop: function() {
this.friends.forEach( // 这个this指向obj
function( friend ) {
console.log(`${this.name} knows ${friend}`);
console.log(this === global); // 在node.js环境下,全局对象为global
}
)
}
}
obj.loop();
// ()左边是loop,属于obj,因此loop函数中的this指向obj
复制代码
输出
$ node test
undefined knows dear
true
undefined knows cat
true
复制代码
能够看到,在forEach中的this并非期待的那样指向obj,而是指向全局对象了
能够用上面提到的,仍是看()左边,在forEach中,()左边是function,而不是一个引用, 因此this指向的就是全局对象
这里其实我也有点迷惑,当不明白Scope的结构时候,能够经过在浏览器中运行代码,去调试面板查看函数执行时的做用域变化。
在构造函数里的this指向
当使用new关键字去执行构造函数时,构造函数中的this指向的的就是新建的那个对象实例。
var savedThis;
function Constr() {
// 保存构造函数中的this
savedThis = this;
}
// 经过new关键字执行构造函数
var inst = new Constr();
// 构造函数中的this指向的就是新建立的对象实例inst
console.log(savedThis === inst); // true
复制代码
若是你没有用new关键字去执行构造函数,那么就要分析函数被调用时所属的做用域了
function Point(x, y) {
this.x = x;
this.y = y;
}
var p = Point(7, 5); // 没有用new关键字去执行构造函数!
console.log(p === undefined); // 没有用new,因此构造函数没有返回一个实例对象, 因此p === undefined
// 没有用new关键字,Point(7,5);就只是把函数执行了一遍
// ()左边是Point,属于全局对象,因此this指向全局对象
console.log(x); // 7
console.log(y); // 5
复制代码
在事件处理器(event handler)中this的指向
<div id="test">I am an element with id #test</div>
function doAlert() {
alert(this.innerHTML);
}
doAlert(); // undefined
// doAlert()属于全局对象
var myElem = document.getElementById('test');
myElem.onclick = doAlert;
alert(myElem.onclick === doAlert); // true
myElem.onclick(); // I am an element
// ()左边是onclick也就是doAlert,属于myElem,因此this指向myElem
复制代码
那个元素触发事件,this就指向那个元素
总结
以上,对于函数中的this,经过查看()左边所属的对象去肯定,真的很好用。
而实质上,this是在建立函数的执行环境时,在建立阶段肯定的,所以,弄透执行环境,去思考执行环境建立阶段的this的指向,this的指向就不会弄错了吧。