40 – 真正理解 Bind、Call 和 Apply

40 – 真正理解 Bind、Call 和 Apply

原文地址:https://dev.to/bhagatparwinder/truly-understand-bind-call-apply-21da

Bind

JavaScript 中的 this 扮演者重要的角色,在 JavaScript 中它是依据函数是怎么被调用的而不是它在哪声明的(箭头函数则表现为相反)。

我们举一个例子来示范一下 this 是如何工作的:

const sayGreeting = { name: “Parwinder”, hello: function() { return `Hello, ${this.name}`; }}console.log(sayGreeting.hello()); // Hello, Parwinder

hello 方法可以获取 sayGreeting 上的 name 属性,当我用 sayGreeting 调用它时,它是运行在 sayGreeting 对象上下文中的。

相反如果我这样做:

const sayGreeting = { name: “Parwinder”, hello: function() { return `Hello, ${this.name}`; }}const hello = sayGreeting.hello;console.log(hello === sayGreeting.hello); // trueconsole.log(hello()); // Hello, undefined

尽管 hello 变量与 sayGreeting 对象上的 hello 方法相等,但它的执行上下文并不是在 sayGreeting 中,而是在 window 或全局状态中。

bind 方法允许我们绑定上下文,它返回一个新的方法且上下文绑定为我们传递给 bind 函数的内容。

const sayGreeting = { name: “Parwinder”, hello: function() { return `Hello, ${this.name}`; }}const hello = sayGreeting.hello.bind(sayGreeting);console.log(hello()); // Hello, Parwinder

实际业务中哪里需要使用 bind?

上面的所有例子,数据的获取和方法的调用都在一个对象上 bind 的作用并不明显。可有时候当你需要向一个对象借一个方法但运行上下文需要在另一个对象中时,你就需要使用 bind。

const sayGreeting = { name: “Parwinder”, hello: function () { return `Hello, ${this.name}`; }}const nameObject = { name: “Lauren”}const hello = sayGreeting.hello.bind(nameObject);console.log(hello()); // Hello, Lauren

sayGreeting 对象上有 hello 方法,所以在 nameObject 对象上就没有必要再写一个相同的方法。我可以向 sayGreeting 对象借用它然后运行在 nameObject 上下文中

Call

call 和 apply 与 bind 是有区别的,bind 返回一个新的方法而 call 和 apply 则立即调用执行方法。call 把 this 作为第一个参数,其他参数需要一个个的传递。它们都会传递到我们调用的函数中:

const sayGreeting = { name: “Parwinder”, hello: function () { return `Hello, ${this.name}`; }}console.log(sayGreeting.hello.call(sayGreeting)); // Hello, Parwinder

带有其它参数:

const sayGreeting = { name: “Parwinder”, hello: function (trait, color) { return `Hello, ${this.name}. I see you ${trait} ${color}. It is my favorite too!`; }}console.log(sayGreeting.hello.call(sayGreeting, “like”, “red”));// Hello, Parwinder. I see you like red. It is my favorite too!

Apply

尽管 apply 和 call 类似都是直接执行函数,但它接受的是一个数组作为第二个参数而不是逗号分隔的值。

const sayGreeting = { name: “Parwinder”, hello: function () { return `Hello, ${this.name}`; }}console.log(sayGreeting.hello.apply(sayGreeting)); // Hello, Parwinder

当没有其余参数时 apply 和 call 没有区别,但是当我们有其余参数时,使用方法就有点区别:

const sayGreeting = { name: “Parwinder”, hello: function (trait, color) { return `Hello, ${this.name}. I see you ${trait} ${color}. It is my favorite too!`; }}console.log(sayGreeting.hello.apply(sayGreeting, [“like”, “red”]));// Hello, Parwinder. I see you like red. It is my favorite too!

apply 使得传递多个参数变得更容易些,但是现在 ES6 中使用扩展运算符传递多个参数也很容易了。

郑重声明:本文内容及图片均整理自互联网,不代表本站立场,版权归原作者所有,如有侵权请联系管理员(admin#wlmqw.com)删除。
(0)
用户投稿
上一篇 2022年6月21日
下一篇 2022年6月21日

相关推荐

  • 能回收京东e卡的平台有哪些?

    对于京东商城大家应该都十分熟悉,京东作为国内电商的龙头。在电商行业内可以说作为标杆也不为过。 京东一直以来也都是大家最常用的购物平台之一。 网购也随着社会的发展网购将成为进一步主流…

    2022年6月15日
  • 3分钟弄明白新老宫本小细节,360度格挡,无限护盾

    “新”宫本上线正式服 “新老”宫本技能对比详解 “老”宫本 VS “新”宫本 01 新老宫本武藏技能对比 相同点 新老宫本1技能空明斩施法时的判定都是360度,而不是只有前面。 背…

    2022年7月1日
  • 库克开了Rivian电动皮卡,外界立刻“脑补”:苹果想干嘛?难道想收购?

    周五,据媒体报道,苹果CEO库克于本周出席了一年一度的太阳谷峰会,在峰会上,他“注意”到了Rivian新推出的R1T电动皮卡,外界便立刻“脑补”,如果苹果收购Rivian会什么样呢…

    2022年7月10日
  • 孤鸾年能不能结婚 女命带孤鸾是什么意思

    孤鸾年结婚好不好,这个词一直都困扰着我们毕竟孤鸾年出现的次数还是蛮多的,所以刚好在这一年打算结婚的朋友犯了难,到底是延后结婚,还是不管不顾呢?想知道孤鸾年能不能结婚,女命带孤鸾是什…

    2022年7月28日
  • iPhone14Pro参数曝光,古铜配色引发关注

    6月23日,据相关爆料,iPhone14 Pro系列的详细参数已经曝光,顶配Max版,机身宽度为78.53mm,长度为 160.71mm,厚度为12.16mm,药丸挖孔直径为7.1…

    2022年6月26日
  • 大S:这次赌对了

    大S与具俊晔官宣后,大S声称退出娱乐圈,说:“幻灭就幻灭,因为生米已经煮成熟饭,管不了那么多,先爱了再说!”但没过多久,大S又打脸高调宣布复出,到底是为了蹭热度圈钱?形势所迫?还是…

    2022年6月29日
  • 草龟怎么养更有灵性(养小草龟的正确方法)

    【小编按】:有朋友留言,要鸫龟先生介绍下养草龟的全套解决方案。鸫龟先生手头养有一只草龟,也有十多年了,正好借此机会,系统总结下心得体会。网络上关于养草龟的方法很多,大家一搜即可。鸫…

    2022年4月19日
  • 岁月渐深,善待老去的自己

    流光容易把人抛,红了樱桃,绿了芭蕉。 变老似乎是突然之间的事情,头发白了几根,眼角有了皱纹,让人感慨,令人伤怀。 三毛说: 还没有来得及认真地年轻过,就认真地老去了。 时光无情,韶…

    2022年5月7日
  • 咸鱼翻身-本泽马

    2021-2022赛季,整个足坛收获最大的球员应该是皇家马德里的本泽马了,个人拿到欧冠冠军,西甲冠军,世俱杯冠军,又拿到欧冠的金靴(踢进15个),应该是今年的金球奖的获得者了,没有…

    2022年8月17日
  • 什么是存储过程?为什么要写存储过程

    一.什么是存储过程 存储过程,百度百科上是这样解释的,存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的SQL 语句集,存储在数据库中,经过第…

    2022年6月18日

联系我们

联系邮箱:admin#wlmqw.com
工作时间:周一至周五,10:30-18:30,节假日休息