关于 this 指向的理解
Created|Updated
|Word Count:1.3k|Reading Time:5mins|Post Views:
函数内 this 的指向
这些 this 的指向,是当我们调用函数的时候确定的。
调用方式的不同决定了this 的指向不同一般指向我们的调用者.
1. 普通函数 this 指向window
1 2 3 4
| function fn() { console.log('普通函数的this' + this); } window.fn();
|
2. 对象的方法 this指向的是对象
o
1 2 3 4 5 6
| var o = { sayHi: function() { console.log('对象方法的this:' + this); } } o.sayHi();
|
3.
构造函数 this 指向 ldh 这个实例对象 原型对象里面的this 指向的也是
ldh这个实例对象
1 2 3 4
| function Star() {}; Star.prototype.sing = function() { } var ldh = new Star();
|
4.
绑定事件函数 this 指向的是函数的调用者 btn这个按钮对象
1 2 3 4
| var btn = document.querySelector('button'); btn.onclick = function() { console.log('绑定时间函数的this:' + this); };
|
5. 定时器函数 this
指向的也是window
1 2 3
| window.setTimeout(function() { console.log('定时器的this:' + this); }, 1000);
|
6. 立即执行函数
this还是指向window
1 2 3
| (function() { console.log('立即执行函数的this' + this); })();
|
改变函数内部 this 指向
JavaScript 为我们专门提供了一些函数方法来帮我们更优雅的处理函数内部
this 的指向问题,常用的有 bind()、call()、apply() 三种方法。
1. call 方法
call()
方法调用一个对象。简单理解为调用函数的方式,但是它可以改变函数的 this
指向。
1
| fun.call(thisArg, arg1, arg2, ...)
|
thisArg:在 fun 函数运行时指定的 this 值
arg1,arg2:传递的其他参数
返回值就是函数的返回值,因为它就是调用函数
因此当我们想改变 this 指向,同时想调用这个函数的时候,可以使用
call,比如继承
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| var o = { name: 'andy' }
function fn(a, b) { console.log(this); console.log(a + b);
}; fn.call(o, 1, 2);
function Father(uname, age, sex) { this.uname = uname; this.age = age; this.sex = sex; }
function Son(uname, age, sex) { Father.call(this, uname, age, sex); } var son = new Son('刘德华', 18, '男'); console.log(son);
|
2. apply 方法
apply()
方法调用一个函数。简单理解为调用函数的方式,但是它可以改变函数的 this
指向。
1
| fun.apply(thisArg, [argsArray])
|
thisArg:在fun函数运行时指定的 this 值
argsArray:传递的值,必须包含在数组里面
返回值就是函数的返回值,因为它就是调用函数
因此 apply 主要跟数组有关系,比如使用 Math.max() 求数组的最大值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| var o = { name: 'andy' };
function fn(arr) { console.log(this); console.log(arr); }; fn.apply(o, ['pink']);
var arr = [1, 66, 3, 99, 4]; var arr1 = ['red', 'pink'];
var max = Math.max.apply(Math, arr); var min = Math.min.apply(Math, arr); console.log(max, min);
|
3. bind 方法
bind() 方法不会调用函数。但是能改变函数内部this 指向
1
| fun.bind(thisArg, arg1, arg2, ...)
|
thisArg:在 fun 函数运行时指定的 this 值
arg1,arg2:传递的其他参数
返回由指定的 this 值和初始化参数改造的原函数拷贝
因此当我们只是想改变 this 指向,并且不想调用这个函数的时候,可以使用
bind
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| var o = { name: 'andy' };
function fn(a, b) { console.log(this); console.log(a + b);
}; var f = fn.bind(o, 1, 2); f();
var btns = document.querySelectorAll('button'); for (var i = 0; i < btns.length; i++) { btns[i].onclick = function() { this.disabled = true; setTimeout(function() { this.disabled = false; }.bind(this), 2000); } }
|
call apply bind 总结
相同点
都可以改变函数内部的this指向.
区别点
call 和 apply 会调用函数, 并且改变函数内部this指向.
call 和 apply 传递的参数不一样, call 传递参数 aru1, aru2..形式 apply
必须数组形式[arg]
bind 不会调用函数, 可以改变函数内部this指向.
主要应用场景
call 经常做继承.
apply 经常跟数组有关系. 比如借助于数学对象实现数组最大值最小值
bind 不调用函数,但是还想改变this指向. 比如改变定时器内部的this指向.