函数原型中的 call 和 apply 方法的区别

call, apply都属于Function.prototype的方法

它们是在 JavaScript 引擎内在实现的,由于属于Function.prototype,因此每一个Function对象实例,也就是每一个方法都有call, apply属性。它们的做用同样,只是使用方式不一样。git

call 与 apply 调用参数不一样

不一样之处在于调用apply函数时,参数能够使用数组; call要求明确列出参数。es6

助记法: Apply 的A表示 Array, 即数组, 而 Call 的 C 表示 Comma, 即逗号。

更多请参阅MDN的文档。github

伪语法:数组

theFunction.apply(valueForThis, arrayOfArgs)
theFunction.call(valueForThis, arg1, arg2, ...)

从ES6开始,还有展开spread数组与该call功能一块儿使用的可能性,你能够在这里看到兼容性。app

示例代码:函数

function theFunction(name, profession) {
    console.log("My name is " + name + " and I am a " + profession +".");
}
theFunction("John", "fireman");
theFunction.apply(undefined, ["Susan", "school teacher"]);
theFunction.call(undefined, "Claude", "mathematician");
theFunction.call(undefined, ...["Matthew", "physicist"]); // 使用展开语法

搞这么复杂,直接调用函数很差吗?

主要是为了模拟面向对象,对状态进行封装的同时, 不一样实例能够有不一样的内部状态,如:this

var module = {
  x: 42,
  getX: function() {
    return this.x;
  }
}

var unboundGetX = module.getX;
console.log(unboundGetX()); // 函数在全局范围内调用,this=window
// 会输出: undefined, 由于window下没有定义x
unboundGetX.call(module) //输出 42, 或使用 bind 也有一样的效果
var module1 ={
   x:123,
   getX: unboundGetX  //this 变为module1
}
module1.getX() //返回123
相关文章
相关标签/搜索