每一个函数都有它本身的this
值,在绝大多数状况下,this
指向调用了函数的那个对象。
为了本身更加清楚地认识this
的指向,我总结出了如下几种状况:app
this
不管是否处于严格模式,在全局环境中(或者理解为在任何函数体的外部)this
都指代全局对象:函数
console.log(this); //全局对象 var num = 100; console.log(this.num); // 100
this
在全局环境中调用函数,函数内部的this
指向有如下两种状况:this
在非严格模式下,this
的值等于全局对象:指针
function temp(){ return this; }; console.log(temp()); //全局对象
在严格模式下,因为this
并无被提早定义,因此,this
的值等于undefined
:code
function temp(){ "use strict"; return this; }; console.log(temp()); //undefined
用apply和call方法能够指定this
的值:对象
var obj = { name: 'Tom' }; function temp(){ "use strict"; return this; }; console.log(temp.call(obj)); //{name: "Tom"}
补充知识点:在严格模式下,经过this
传递给一个函数的值不会被强制转换为一个对象:继承
function temp(){ "use strict"; return this } console.log(temp.call(true)); // true console.log(temp.call(56)); // 56 console.log(temp.apply(null)); //
this
箭头函数不会建立本身的this
,它只会从本身所处的做用域链的上一层继承this
。事件
例1:箭头函数没有本身的this
指针,经过call或apply方法调用一个箭头函数时,为this
绑定特定的值是无效的:ip
var name = 'window'; var obj = { name: 'Tom' }; var temp = () => { return this.name; }; console.log(temp.call(obj)); //window
箭头函数是在全局环境中调用的,它上一层的this
指向全局对象,因此,它的this
也指向全局对象。作用域
例2:在函数内部建立的箭头函数,其this
指向等同于包含函数的this
指向:
name = 'window'; let obj = { name: 'Tom', test: function(){ let temp = (()=>{ return this.name; }); return temp; } }; console.log(obj.test()()); //Tom
包含函数做为对象里面的方法被调用时,它的this
指向调用它的对象obj,因此,箭头函数的this
也指向obj。
name = 'window'; let obj = { name: 'Tom', test: function(){ let temp = (()=>{ return this.name; }); return temp; } }; let a = obj.test; console.log(a()()); //window
包含函数被赋值给一个全局变量,以后再在全局环境中调用,显然,此时它的this
指向调用它的全局对象,因此,箭头函数的this
也指向全局对象。
例3:明白了箭头函数的this
指向原理,在回调函数中就不用写这样的代码了:var that = this,这里以setTimeout的回调函数为例:
不用箭头函数:
var name = "outer"; var obj = { name: 'Tom' }; function temp(){ let that = this; setTimeout(function(){ console.log(that.name); },1000); } temp.call(obj); //Tom
使用箭头函数:
var name = "outer"; var obj = { name: 'Tom' }; function temp(){ setTimeout(() => { console.log(this.name); },1000); } temp.call(obj); // Tom
this
对象中函数的this指向调用函数的那个对象, 而且是离得最近的那个对象:
name = 'window'; let obj1 = { name: '1', test: function(){ return this.name; }, other: { name: '2' } }; obj1.other.test = obj1.test; console.log(obj1.test()); // 1 console.log(obj1.other.test()); //2 let aa = obj1.test; console.log(aa()); //全局对象
this
构造函数中的this指向建立的新对象:
function Person(name){ this.name = name; }; let child = new Person('Tom');
补充知识点:new的过程到底发生了什么:
this
指向事件发生的DOM元素:
<div> <button id="btn" onclick="alert(this)">alert</button> // 在弹出框中显示:btn </div>
this
当一个函数被用做事件处理函数时,它的this
指向触发事件的元素:
<div> <input type="button" id="btn" value="Click Me"/> </div> <script> var btn = document.getElementById('btn'); btn.addEventListener('click', function(){ console.log(this.id); // btn }, false); </script>