【问题标题】:What's the difference between bind and call? [duplicate]绑定和调用有什么区别? [复制]
【发布时间】:2014-09-02 20:37:01
【问题描述】:

似乎 bind 和 call 做同样的事情。每个人都有优势/劣势吗?

<script type="text/javascript">

    var x = {

        name : "test"

    }
     function a1() {

        a2.bind(x)();            a2.call(x);

    }

    function a2() {

        console.log(this);
    }

    a1();  // output x object.
</script>

【问题讨论】:

  • bindcall 可以以不同的方式使用来实现相同的效果,对于某些特定的效果子集。这与“他们做同样的事情”不同。如果他们做同样的事情,我相信现在有人会注意到并从标准中删除其中一个。

标签: javascript


【解决方案1】:

.call 立即执行该函数。
.bind 返回一个可以在您方便时执行的新函数。例如,它可以用作回调。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-09
    • 1970-01-01
    • 2017-05-22
    • 2015-11-23
    • 2013-02-13
    • 2011-04-26
    • 2016-09-24
    相关资源
    最近更新 更多