JavaScript的call,apply和bind方法之间的区别

by Rajat Saxena

通过拉贾特·萨克森纳(Rajat Saxena)

JavaScript的call,apply和bind方法之间的区别 (The difference between JavaScript’s call, apply, and bind methods)

让我们将其深入研究,并一劳永逸地完成它。 (Let’s drill it into our minds and be done with it, once and for all.)

I’m writing this micro post because the aforementioned question has haunted me for a very long time, and I knew I wasn’t the only one. Every single time I saw someone use any of those three methods I had to rush to MDN in order to figure out what was going on.

我之所以写这篇微型文章,是因为上述问题困扰了我很长时间,而且我知道我并不是唯一的一个。 每当我看到有人使用这三种方法中的任何一种时,我都必须赶紧向MDN进行操作以弄清发生了什么。

Enough was enough. I knew I had to do something and I did. I supposedly have created a proverbial silver bullet which can help new JavaScript developers out.

足够了。 我知道我必须做某事,而且我做到了。 据推测,我创造了一个众所周知的灵丹妙药,可以帮助新JavaScript开发人员。

Disclaimer: This is not a theoretical solution, but a really hacky way to remember the difference.

免责声明 :这不是理论上的解决方案,而是一种记住差异的真正卑鄙的方式。

The major cause of confusion between the call() and apply() methods is how to pass in the additional arguments besides this. And why do we have bind() anyway?

在混淆的主要原因call() ,并apply()方法是如何在除了额外的参数传递this 。 为什么我们仍然有bind()

So let’s learn how to easily tell the three apart.

因此,让我们学习如何轻松区分这三个。

应用() (Apply())

apply(this [, [arg1, arg2,...]]): Calls a function with a provided this value. Further arguments are provided as a single array.

apply(this [, [arg1, arg2,...]]) 使用提供的this值调用一个函数。 其他参数作为单个数组提供

Way to remember: “Apply accepts arguments as an Array” or “AA

的方式来记住 :“A pply接受参数为A rray”或“AA”

呼叫() (Call())

call(this [, arg1, arg2...]): Calls a function with a provided this. Further arguments are provided as a comma separated list

call (this [, arg1, arg2...]) 使用提供的this调用函数。 其他参数以逗号分隔列表的形式提供

Ways to remember: “Call’s arguments are separated by commas” or “CC”.

记住方式: “ Call的参数用逗号分隔”或“ CC ”。

绑定() (Bind())

bind(this): Returns a new function whose this value is bound to the provided value.

bind(this) 返回一个新的函数,其this值被绑定到所提供的值。

Ways to remember: bind() is the only method out of the three that returns a new function altogether. It does not call the function.

记住方式: bind()是这三个方法中唯一一个完全返回新函数的方法。 它不调用该函数。

结语 (Wrap up)

I hope the above explanation might help some of you out there. It certainly is helping me.

希望以上解释对您有所帮助。 它肯定在帮助我。

Do you have other memorization tricks related to programming? Kindly share it with the community as it will help everyone out. Especially during those interviews.

您还有其他与编程有关的记忆技巧吗? 请与社区分享,因为它将帮助所有人。 特别是在那些采访中。

If you have any queries or doubts, hit me up on Twitter @rajat1saxena or write to me at rajat@raynstudios.com. Please recommend this post, if you liked it and share it with your network.

如果您有任何疑问或疑问,请在Twitter @ rajat1saxena上打我,或给我写信至rajat@raynstudios.com 。 如果喜欢,请推荐这篇文章,并与您的网络分享。

翻译自: https://www.freecodecamp.org/news/the-difference-between-javascripts-call-apply-and-bind-methods-4e69917f77bd/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值