【问题标题】:When and why to use Call and Apply?何时以及为何使用 Call and Apply?
【发布时间】:2014-05-14 08:55:47
【问题描述】:

首先我知道了 apply() 和 call() 的区别。

function theFunction(name, profession) {
    alert("My name is " + name + " and I am a " + profession + ".");
}

theFunction("John", "fireman");
theFunction.apply(undefined, ["Susan", "school teacher"]); // This call be called theFunction("Susan", "school teacher");, why use apply
theFunction.call(undefined, "Claude", "mathematician"); // This call be called theFunction("Claude", "mathematician");, why use call 

从上面的代码中,所有 3 个函数调用都会显示警报消息。

  1. 与普通函数调用相比,使用apply和call有什么优点/缺点,什么时候使用apply/call合适,请澄清一下。

  2. 还有一件事,如果函数是基于原型的函数怎么办:

Function.prototype.theFunction = 函数(姓名,职业){

    alert("My name is " + name + " and I am a " + profession + ".");
}

那么如何使用 apply 或 call 来调用这个函数。我试过这样:

theFunction.apply(undefined, ["Susan", "school teacher"]); 
theFunction.call(undefined, "Claude", "mathematician"); 

但导致错误。 "ReferenceError: theFunction is not defined"

【问题讨论】:

    标签: javascript prototype-programming


    【解决方案1】:

    正如您所说,您似乎已经知道这些函数 apply()call() 的实际作用,但就它们的用途而言,我想说它们主要用于您想要提供 @987654323 @ 使用您自己的特定对象,作为其上下文中的 this 值。

    这两个最流行的用途之一是在函数中处理类似数组的对象,如arguments 对象:

    function(){
        //let's say you want to remove the first parameter from the arguments object
    
        //you can make sure that
        console.log(arguments instanceof Array);//false
    
        //as you see arguments is not an actual array object but it is something similar
        //and you want slice out its value
        var myparams = Array.prototype.slice.call(arguments, 1);
    
        //here you have myparams without your first argument
    
        console.log(arguments);
    }
    

    让我们再举一个例子。假设我们有一个独立的函数,例如:

    function getName(){
        console.log(this.name);
    }
    

    现在您可以将它用于任何类型的具有 name 属性的 JavaScript 对象:

    var myInfo = {
        name: 'SAM'
    };
    

    现在如果你这样做:

    getName.call(myInfo);
    

    它的作用是打印出name 属性,或者您可以在函数本身上尝试:

    getName.call(getName);
    

    这将在控制台中打印出函数的名称 ("getName")。

    但与我的第一个示例类似,它通常用于当您要使用不在对象原型链中的函数时。另一个例子可能是:

    //let's say you have an array
    var myArray = [1 , 2];
    //now if you use its toString function
    console.log(myArray.toString());//output: "1,2"
    
    //But you can use the Object toString funcion
    //which is mostly useful for type checking
    console.log(Object.prototype.toString.call(myArray));//output: "[object Array]"
    

    【讨论】:

      【解决方案2】:

      这个post 对 call() 和 apply() 给出了非常详细的解释。

      TLDR;

      call() 和 apply() 都是我们可以用来分配 this 的方法 方法调用期间的指针

      apply() 方法与 call() 相同,除了 apply() 需要一个 数组作为第二个参数。数组表示参数 目标方法。

      【讨论】:

        【解决方案3】:

        主要区别在于apply 允许您以数组形式调用函数; call 要求明确列出参数。

        它会给你更多的解释 POST

        【讨论】:

        • 亲爱的朋友,我知道,正如我在我的问题中提到的,如果你知道除此之外,请告诉我
        • apply() 方法很有用,因为我们可以构建一个类似于 createDelegate 的函数,它不关心目标方法的签名。该函数可以使用 apply() 通过数组将所有附加参数传递给目标方法。使用 call 的函数调用比使用 apply 稍快
        猜你喜欢
        • 2012-03-28
        • 1970-01-01
        • 2012-05-02
        • 2020-07-22
        • 1970-01-01
        • 1970-01-01
        • 2013-11-08
        • 1970-01-01
        相关资源
        最近更新 更多