apply、call 的区别和用途

11,331
原文链接: www.yaya12.com

作为一个前端程序媛,在提升学习的道路上,不可避免的与apply和call相遇了。之前由于它俩出镜率有点低,都静静的擦肩而过了!今天不小心被它俩的魅力所吸引,加上本小姐心情好,就让我们好好的相识一下吧O(∩_∩)O~

ECAMScript 3给Function的原型定义了两个方法,它们是Function.prototype.call和Function.prototype.apply。

一.call和apply的区别

1、Function.prototype.call 和 Function.prototype.apply 都是非常常用的方。它们的作用一模一样,区别仅在于传入参数的形式的不同。

apply 接受两个参数,第一个参数指定了函数体内 this 对象的指向,

第二个参数为一个带下标的集合,这个集合可以为数组,也可以为类数组,apply 方法把这个集合中的元素作为参数传递给被调用的函数

var func = function( a, b, c ){ 
    console.log([a,b,c]); //输出:[1,2,3]
};
func.apply( null, [ 1, 2, 3 ] );

call 传入的参数数量不固定, 跟apply 相同的是,第一个参数也是代表函数体内的 this 指向, 从第二个参数开始往后,每个参数被依次传入函数

var func = function( a, b, c ){ 
    console.log([a,b,c]); //输出:[1,2,3]
};
func.call( null, 1, 2, 3 );

2、当使用 call 或者 apply 的时候,如果我们传入的第一个参数为 null,函数体内的 this 会指 向默认的宿主对象,在浏览器中则是 window

var func = function( a, b, c ){ 
    console.log(this === window); // 输出:true
};
func.apply( null, [ 1, 2, 3 ] );

但如果是在严格模式下,函数体内的 this 还是为 null

var func = function( a, b, c ){ 
    "use strict";
    console.log(this === null); // 输出:true
};
func.apply( null, [ 1, 2, 3 ] );

3、有时候我们使用 call 或者 apply 的目的不在于指 定this 指向,而是另有用途,比如借用其他对象的方法。那么我们可以传入 null 来代替某个具体的对象

var a=Math.max.apply( null, [ 1, 2, 5, 3, 4 ] );
console.log(a);// 输出:5

二.call和apply的用途

1. 改变this指向

例一

var obj1={ 
    name: 'sven'
};
var obj2={ 
    name: 'anne'
};
window.name = 'window';
var getName = function(){ 
    console.log ( this.name );
};
getName(); // 输出: window
getName.call( obj1 );// 输出: sven
getName.call(obj2 ); // 输出: anne

其中在执行

<strong>getName.call( obj1 );</strong>

时,类似于执行

<strong>var getName = function(){ 
    console.log ( obj1.name );// 输出: sven
}; </strong>

例二

document.getElementById( 'div1' ).onclick = function(){
    console.log( this.id );// 输出: div1
    var func = function(){ 
        console.log ( this.id );// 输出: undefined
    } 
    func();
}; 
//修正后
document.getElementById( 'div1' ).onclick = function(){
    var func = function(){ 
        console.log ( this.id );// 输出: div1
    } 
    func.call(this);
}; 

2. Function.prototype.bind

Function.prototype.bind = function( context ){ 
    var self = this; // 保存原函数
    return function(){ // 返回一个新的函数
        return self.apply( context, arguments );//执行新的函数的时候,会把之前传入的context当作新的函数体的this
    } 
};
var obj={ 
    name: 'sven'
};
var func = function(){ 
    console.log ( this.name );// 输出: sven
}.bind( obj); 
func();
//复杂化
Function.prototype.bind = function(){ 
    var self = this, // 保存原函数
        context = [].shift.call( arguments ),//需要绑定的this上下文
        args = [].slice.call( arguments ); //剩余的参数转成数组
    return function(){ // 返回一个新的函数
        return self.apply( context, [].concat.call( args, [].slice.call( arguments ) ) ); 
        //执行新的函数的时候,会把之前传入的context当作新的函数体的this
        //并且组合两次分别传入的参数,作为新的函数的参数
    } 
};
var obj={ 
    name: 'sven'
};
var func = function( a, b, c, d ){
    console.log ( this.name ); // 输出: sven 
    console.log([a,b,c,d]) //输出: [1,2,3,4]
}.bind( obj, 1, 2 ); 
func( 3, 4 );

3. 借用其他对象的方法

借用方法的第一种场景是“借用构造函数”,通过这种技术,可以实现一些类似继承的效果

var A = function( name ){ 
    this.name = name;
};
var B = function(){ 
    A.apply(this,arguments);
};
B.prototype.getName = function(){ 
    return this.name;
};
var b=new B('sven');
console.log( b.getName() ); // 输出:  'sven'

借用方法的第二种场景——函数的参数列表 arguments 是一个类数组对象,虽然它也有“下标”,但它并非正的数组,所以也不能像数组一样,进行排序操作或者往集合里添加一个新的元素。

这种情况下,我们常常 会借用 Array.prototype 对象上的方法。

比如想往 arguments 中添加一个新的元素,通常会借用 Array.prototype.push;

想把 arguments 转成真正的数组的时候,可以借用 Array.prototype.slice 方法;

想截取arguments 列表中的一个元素时,可以借用 Array.prototype.shift 方法。

var a={};
Array.prototype.push.call( a, 'first' );
console.log ( a.length ); // 输出: 1 
console.log(a[0]); //输出: first
//这段代码在大部分浏览器里都能顺利执行,但由于引擎的内部实现存在差异,如果在低版本的 IE浏览器 中执行,必须显式地给对象 a 设置 length属性
var a={ 
    length: 0
}; 

借用 Array.prototype.push 方法的对象还要满足以下两个条件
1、对象本身要可以存取属性
2、对象的 length 属性可读写。

360云盘代码下载:yunpan.cn/ckcdvDALGAk… (提取码:d76d)