Fork me on GitHub

JavaScript中call,apply,bind联系与区别

call/apply/bind方法的来源:

call,apply,bind这三个方法都是继承自Function.prototype中的,属于实例方法。

1
2
3
console.log(Function.prototype.hasOwnProperty('call')) //true
console.log(Function.prototype.hasOwnProperty('apply')) //true
console.log(Function.prototype.hasOwnProperty('bind')) //true

Function.prototype.call()

函数实例的call方法,可以指定该函数内部this的指向,然后在所指定的作用域中,调用该函数。并且会立即执行该函数。如:

1
2
3
4
5
6
7
8
9
10
11
12
13
var zhangsan = {
age: 20 //;会报错Uncaught SyntaxError: Unexpected token
};
var age = 22;
function a() {
console.log(this.age);
}
a(); //22
a.call(); //22
a.call(null); //22
a.call(undefined); //22
a.call(this); //22
a.call(zhangsan); //20

上述代码中,this如果指向全局对象,则返回22,否则返回20。

call()方法可以传递2个参数,第一个参数是函数执行时所在的作用域,第二个是函数调用时需要传递的参数。第一个参数时必须的,可以是null,undefined,this,其表示函数此时处于全局作用域,第二个参数必须一个个添加。而在apply()方法中,第二个参数必须为数组。

call()方法的应用:调用对象的原生方法或将类数组对象转换为数组

1
2
3
4
5
6
7
var obj = {};
console.log(obj.hasOwnProperty('toString')); //false
obj.hasOwnProperty = function() {
return true;
}
console.log(obj.hasOwnProperty('toString')); //true
console.log(Object.prototype.hasOwnProperty.call(obj, 'toString')); //false

hasOwnProperty是obj对象继承的方法,如果这个方法一旦被覆盖,就不会得到正确结果。call方法可以解决这个方法,它将hasOwnProperty方法的原始定义放到obj对象上执行,这样无论obj上有没有同名方法,都不会影响结果。要注意的是,hasOwnProperty是Object.prototype原生对象的方法,而call是继承自Function.prototype的方法。

Function.prototype.apply()

apply方法的作用与call方法类似,也是改变this指向(函数执行时所在的作用域),然后在指定的作用域中,调用该函数。同时也会立即执行该函数。唯一的区别就是,它接收一个数组作为函数执行时的参数

call与apply的区别:

1
2
3
4
5
function keith(a, b) {
console.log(a + b);
}
keith.call(null, 2, 3); //5
keith.apply(null, [2, 3]); //5

有必要再说一遍:call()第二个参数必须一个个添加【可同时添加多个】,二apply()必须以数组的形式添加。

apply()应用:

  • 找出数组中的最大数

    1
    2
    3
    4
    var a = [2, 4, 5, 7, 8, 10];
    console.log(Math.max.apply(null, a)); //10
    console.log(Math.max.call(null,2, 4, 5, 7, 8, 10)); //10
    Javascript中是没有提供找出数组中最大值的方法的,使用继承自Function.prototype的apply和Math.max方法,就可以返回数组的最大值。
  • 将数组的空元素变为undefined【利用Array构造函数将数组的空元素变成undefined。】

    1
    console.log(Array.apply(null, [1, , 3])); // [1, undefined, 3]
  • 空元素与undefined的区别在于,数组的forEach()会跳过空元素,但不会跳过undefined和null。

1
2
3
4
5
6
7
var a = [1, , 3];
a.forEach(function(index) {
console.log(index); //1,3 ,跳过了空元素。
})
Array.apply(null,a).forEach(function(index){
console.log(index); ////1,undefined,3 ,将空元素设置为undefined
})

利用数组对象的slice方法,可以将一个类似数组的对象(比如arguments对象)转为真正的数组。当然,slice方法的一个重要应用,就是将类似数组的对象转为真正的数组。call和apply都可以实现该应用。

1
2
3
4
5
6
7
8
9
10
11
console.log(Array.prototype.slice.apply({0:1,length:1})); //[1]
console.log(Array.prototype.slice.call({0:1,length:1})); //[1]
console.log(Array.prototype.slice.apply({0:1,length:2})); //[1,undefined×1]
console.log(Array.prototype.slice.call({0:1,length:2})); //[1,undefined×1]
//n为任意数字
console.log(Array.prototype.slice.apply({0:1,length:n})); //[1,undefined×(n-1)]
console.log(Array.prototype.slice.call({0:1,length:n})); //[1,undefined×(n-1)]
function keith(a,b,c){
return arguments;
}
console.log(Array.prototype.slice.call(keith(2,3,4))); //[2,3,4]

该方法起作用的前提:被处理的对象必须有length属性,以及相对应的数字键。

Function.prototype.bind()

bind方法用于指定函数内部的this指向(执行时所在的作用域),然后返回一个新函数。bind方法并非立即执行一个函数。

1
2
3
4
5
6
7
8
9
var keith = {
a: 1,
count: function() {
console.log(this.a++);
}
};
keith.count(); //1
keith.count(); //2
keith.count(); //3

上面代码中,如果this.a指向keith对象内部的a属性,如果这个方法赋值给另外一个变量,调用时就会出错.

1
2
3
4
5
6
7
8
var keith = {
a: 1,
count: function() {
console.log(this.a++);
}
};
var f = keith.count;
f(); //NaN

上面代码中,如果把count方法赋值给f变量,那么this对象指向不再是keith对象了,而是window对象。而window.a默认为undefined,进行递增运算之后undefined++就等于NaN。

为了解决这个问题,可以使用bind方法,将keith对象里的this绑定到keith对象上,或者是直接调用。

1
2
3
4
5
6
7
var f = keith.count.bind(keith);
f(); //1
f(); //2
f(); //3
keith.count.bind(keith)() //1
keith.count.bind(keith)() //2
keith.count.bind(keith)() //3

当然,this也可以绑定到其他对象上。

1
2
3
4
5
6
7
var obj = {
a: 100
};
var f = keith.count.bind(obj);
f(); //100
f(); //101
f(); //102

同样,我们也可以给bind方法传递参数,第一个参数如果为null或者undefined或者this,会将函数内部的this对象指向全局环境;第二个为调用时需要的参数,并且传递参数的形式与call方法相同。

1
2
3
4
5
6
7
function keith(a, b) {
return a + b;
}
console.log(keith.apply(null,[1,4])); //5
console.log(keith.call(null,1,4)); //5
console.log(keith.bind(null, 1, 4)); //keith()
console.log(keith.bind(null, 1, 4)()); //5

绑定回调函数的对象:

如果在回掉函数中使用this对象,那么this对象是会指向DOM对象,也就是button对象。如果要解决回调函数中this指向问题,可以用如下方法。

1
2
3
4
5
6
7
8
9
10
var o = {
f: function() {
console.log(this === o);
}
}
$('#button').on('click', function() {
o.f.apply(o);
//或者 o.f.call(o);
//或者 o.f.bind(o)();
});

点击按钮以后,控制台将会显示true。由于apply方法(或者call方法)不仅绑定函数执行时所在的对象,还会立即执行函数(而bind方法不会立即执行,注意区别),因此不得不把绑定语句写在一个函数体内。

call,apply,bind方法的联系和区别:

第一个参数都是指定函数内部中this的指向(函数执行时所在的作用域),然后根据指定的作用域,调用该函数。
都可以在函数调用时传递参数。call,bind方法需要直接传入,而apply方法需要以数组的形式传入。
call,apply方法是在调用之后立即执行函数,而bind方法没有立即执行,需要将函数再执行一遍。有点闭包的味道。
改变this对象的指向问题不仅有call,apply,bind方法,也可以使用that变量来固定this的指向。
[原文链接]:(http://www.cnblogs.com/Uncle-Keith/p/5822549.html)

据说帅的人都赏给博主几块钱零花钱。