免费爱碰视频在线观看,九九精品国产屋,欧美亚洲尤物久久精品,1024在线观看视频亚洲

      40 – 真正理解 Bind、Call 和 Apply

      40 – 真正理解 Bind、Call 和 Apply

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

      Bind

      JavaScript 中的 this 扮演者重要的角色,在 JavaScript 中它是依據(jù)函數(shù)是怎么被調用的而不是它在哪聲明的(箭頭函數(shù)則表現(xiàn)為相反)。

      我們舉一個例子來示范一下 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 方法相等,但它的執(zhí)行上下文并不是在 sayGreeting 中,而是在 window 或全局狀態(tài)中。

      bind 方法允許我們綁定上下文,它返回一個新的方法且上下文綁定為我們傳遞給 bind 函數(shù)的內容。

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

      實際業(yè)務中哪里需要使用 bind?

      上面的所有例子,數(shù)據(jù)的獲取和方法的調用都在一個對象上 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 是有區(qū)別的,bind 返回一個新的方法而 call 和 apply 則立即調用執(zhí)行方法。call 把 this 作為第一個參數(shù),其他參數(shù)需要一個個的傳遞。它們都會傳遞到我們調用的函數(shù)中:

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

      帶有其它參數(shù):

      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 類似都是直接執(zhí)行函數(shù),但它接受的是一個數(shù)組作為第二個參數(shù)而不是逗號分隔的值。

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

      當沒有其余參數(shù)時 apply 和 call 沒有區(qū)別,但是當我們有其余參數(shù)時,使用方法就有點區(qū)別:

      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 使得傳遞多個參數(shù)變得更容易些,但是現(xiàn)在 ES6 中使用擴展運算符傳遞多個參數(shù)也很容易了。

      鄭重聲明:本文內容及圖片均整理自互聯(lián)網(wǎng),不代表本站立場,版權歸原作者所有,如有侵權請聯(lián)系管理員(admin#wlmqw.com)刪除。
      (0)
      用戶投稿
      上一篇 2022年6月21日 06:41
      下一篇 2022年6月21日 06:42

      相關推薦

      • “生氣都很可愛”的小句子

        1.給你臺階下,你卻坐電梯。 2.你惹我一次,我放你一馬。但是你要知道,我是你女朋友,不是放馬的。 3.我太容易生氣了,我就應該去幼兒園門口賣氣球。 4.別惹我,惹急了我什么都能做…

        2022年6月19日
      • 中考作文:筆下人物活起來

        聚焦到對人物的描寫,我要教你一些方法,讓你筆下的人物活起來。先體會一下這個小片段,看看這段文字描寫了一個怎樣的人物。 老板娘便抄起刀,碼好冬瓜,嫻熟地比好薄厚,“切這塊,行不!”媽…

        2022年6月19日
      • 國內緊急痛批十大“黑榜”洗發(fā)水!清揚海飛絲上榜 還是國貨最好用

        原來以前用的海飛絲清揚洗發(fā)水 都會導致頭皮出油超多 而且還會有頭皮屑 最近研究了一系列的洗發(fā)水 被國貨洗發(fā)水驚艷到了 尤其是青幽堂去屑洗發(fā)水 洗完頭皮很干凈 三四天不洗頭也不油,頭…

        2022年6月25日
      • 混到最后一個朋友也沒有的,往往有三種人,但愿沒有你

        圖/源自網(wǎng)絡 侵權請聯(lián)系刪除 人生是一場孤獨的旅程,雖然起點和終點都需要我們獨自去經(jīng)歷,但能夠在一路上共同欣賞曼妙風景的人會令人印象深刻。 除去親情的天然聯(lián)結,人生的大部分光陰我們…

        2022年5月1日
      • 阿里跳槽來的,連Spring容器與SpringMVC的容器的區(qū)別都不知道

        說起spring容器和SpringMVC容器,很多剛接觸spring框架的同志都有點懵,甚至是一頭霧水,分不清楚兩者的關系和區(qū)別。這倆容器呢雖然有必然的聯(lián)系,但是他們的區(qū)別也是有的…

        2022年9月17日
      • 《阿凡達》將再次重映 以聲畫重制版登影院

        網(wǎng)易娛樂4月28日報道 據(jù)外媒報道,迪士尼宣布《阿凡達》將(再次)重映,今年9月開始以聲畫重制版登陸全球范圍影院,北美是9月23日。 已定檔今年12月上映的系列第二部則正式定名為《…

        2022年5月12日
      • 日本創(chuàng)意真的有夠絕!這次推出給iPhone充電的杯面充電座

        對于喜歡“新鮮感”或“創(chuàng)意無限”的人,經(jīng)常都會尋找一些意想不到的新品,就像充電座,你有沒有想過是“杯面”造型的呢?而且還有栩栩如生的配料像蚵仔面線、蝦子、肉塊等? 最近日本日清食品…

        2022年6月16日
      • 用100萬現(xiàn)金養(yǎng)老的人會比每個月拿3500元退休金的人過得舒服嗎?

        這事得根據(jù)個人情況而議,歲數(shù)大小,50能跟70多歲人能比嗎?健康人能跟常吃藥的人比嗎? 除非七八十歲的老人,有百八十萬還得舍的花,才過的舒服。 五六十歲的老人,不管健康是否,都是有…

        2022年4月19日
      • 向往“簡單”(隨筆)

        向往“簡單”(隨筆) 作者 王藝淶 摘要:任何事物,如果簡單便捷,就易于讓大多數(shù)人接受,如果尖酸雜復,終將被大多數(shù)人拋棄,這在我們生活中常見,也是最簡單的道理,但這個事實和現(xiàn)象卻?!?/p>

        2022年6月15日
      • 快遞承包區(qū) 的 流水 很可 觀,但是 承包 商 卻 始終沒錢

        大家好,我是默默,以前是一位快遞從業(yè)者,經(jīng)營過菜鳥驛站與幾家快遞公司的承包區(qū)。我想通過自己些許經(jīng)驗分享自己踩過的坑,給后來者一些借鑒! 快遞從業(yè)者每天在外風吹日曬,體力勞動強度大,…

        2022年6月20日

      聯(lián)系我們

      聯(lián)系郵箱:admin#wlmqw.com
      工作時間:周一至周五,10:30-18:30,節(jié)假日休息