美文网首页
call / apply / bind

call / apply / bind

作者: _William_Zhang | 来源:发表于2018-09-23 17:57 被阅读2次
  • call / apply
    fn.call(asThis, p1,p2) 是函数的正常调用方式
    当你不确定参数的个数时,就使用 apply
    fn.apply(asThis, params)

因为参数太多时,用 .call 一个一个传参 要累死的....,不如用 apply ,整体的 apply 进去!


image.png
  • bind
    call 和 apply 是直接调用函数,而 bind 则是返回一个新函数(并没有调用原来的函数),这个新函数会 call 原来的函数,call 的参数由你指定。


    不用bind
用bind

传给 bind 的 this,会作为 onClick.call 的第一个参数。即:bind(this) 等价于 onClick.call(this)。 嗯,可以这么理解。

(mdn之 bind 的文档)https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

相关文章

网友评论

      本文标题:call / apply / bind

      本文链接:https://www.haomeiwen.com/subject/dzkqoftx.html