/**
* 作用:都是在特定的作用域中调用函数时改变this的指向,指向函数的this,非调用者。
* 不同点:apply接收的第二个参数必须是数组。call需要没有这个限制,但是参数要一一列举
* bind不会立即执行
*/
var test = {
name : 'test'
};
var testFun = function(name){
return name != undefined ? this.name=name : this.name;
};
testFun.call(test,'a');
testFun.apply(test,['b']);
testFun.bind(test,'c')();
// bind重复绑定后,this的指向还是第一次的绑定值
// MDN对bind的介绍
bind()
方法创建一个新的函数,在bind()
被调用时,这个新函数的this
被bind的第一个参数指定,其余的参数将作为新函数的参数供调用时使用。
// 下面输出值是1,
// 原因是bind被调用时返回的是一个函数,此时函数是没有执行的,当再次调用bind时,此时传入的参数就是这个函数,再次绑定的结果还是第一个传入的参数。
var test = function() { console.log(this.num) // 输出的是1 } var num = test.bind({num: 1}).bind({num: 5}); num();
// 可以让第一次绑定后立即执行,再次绑定就可以重新绑定了
var test = function() { console.log(this.num); // 打印俩次 第一次是1 第二次是5 return test; // 必须将这个函数返回,undefined不行 } var num = test.bind({num: 1})(); num.bind({num: 5})();
// MDN 提供的bind的兼容性写法 if (!Function.prototype.bind) { Function.prototype.bind = function(oThis) { if (typeof this !== 'function') { // closest thing possible to the ECMAScript 5 // internal IsCallable function throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable'); } var aArgs = Array.prototype.slice.call(arguments, 1), fToBind = this, fNOP = function() {}, fBound = function() { // this instanceof fBound === true时,说明返回的fBound被当做new的构造函数调用 return fToBind.apply(this instanceof fBound ? this : oThis, // 获取调用时(fBound)的传参.bind 返回的函数入参往往是这么传递的 aArgs.concat(Array.prototype.slice.call(arguments))); }; // 维护原型关系 if (this.prototype) { // 当执行Function.prototype.bind()时, this为Function.prototype // this.prototype(即Function.prototype.prototype)为undefined fNOP.prototype = this.prototype; } // 下行的代码使fBound.prototype是fNOP的实例,因此 // 返回的fBound若作为new的构造函数,new生成的新对象作为this传入fBound,新对象的__proto__就是fNOP的实例 fBound.prototype = new fNOP(); return fBound; }; }
call、apply、bind
最新推荐文章于 2024-03-05 14:24:02 发布