dongnuan7956 2009-12-31 19:56
浏览 115
已采纳

电话和申请有什么区别?

使用callapply之间的区别是什么?调用函数?

varfunc=function(){
alert('hello!');
};

func.apply();vsfunc.call();

两者之间是否存在性能差异上述两种方法?何时最好使用call而不是apply,反之亦然?

转载于:https://stackoverflow的.com/问题/1986896/什么-是最差之间呼叫和-适用?页=1&标签=票

  • 写回答

17条回答 默认 最新

  • doudou3213 2009-12-31 20:00
    关注

    不同之处在于apply允许您使用arguments调用该函数作为阵列;call要求显式列出参数。一个有用的助记符是Aarray和Ccomma。”

    请参阅有关申请的MDN文档和致电。

    伪语法:

    theFunction.apply(valueForThis,arrayOfArgs)

    theFunction。call(valueForThis,arg1,arg2,...)

    从ES6开始,还有可能spread数组,用于调用函数,您可以在此处查看兼容性。

    示例代码:



    functiontheFunction(name,profession){
    console.log(”我的名字是“+名字+”,我是“+专业+”。“;;
    }

    TheFunction(”John“,”fireman“);
    theFunction.apply(undefined,[”Susan“,”schoolteacher“]);
    theFunction.call(未定义,“克劳德”,“数学家”);
    theFunction.call(undefined,...[“Matthew”,“物理学家”]);//与扩展运算符一起使用



    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(16条)

报告相同问题?