javascript閉包_通過郵寄包裹解釋JavaScript閉包

javascript閉包

by Kevin Kononenko

凱文·科諾年科(Kevin Kononenko)

通過郵寄包裹解釋JavaScript閉包 (JavaScript Closures Explained by Mailing a Package)

如果您以前寄過包裹或信件,那么您可以了解JavaScript中的閉包。 (If you have mailed a package or letter in the past, then you can understand closures in JavaScript.)

On your journey to becoming an intermediate or advanced JavaScript dev, you may have come across closures. After reading a technical resource on the subject… you also probably ran in the opposite direction.

在成為中級或高級JavaScript開發人員的過程中,您可能會遇到閉包的情況。 閱讀有關該主題的技術資源之后,您可能也朝相反的方向奔跑。

Here is the awesome thing about closures: they allow you to write functions with an intermediate step that can capture data from your site at a specific moment in time. It’s like adding a ‘pause’ button to your function. You can run your function and save the value of a variable at that particular point in time. Then, when you want to resume the function at a later point and use values of variables that have changed within your app… you can do that with a closure, or a function within the original function.

這是關于閉包的很棒的事情 :它們允許您編寫帶有中間步驟的函數,該中間步驟可以在特定時間捕獲站點中的數據。 這就像在功能中添加“暫停”按鈕一樣。 您可以運行函數并在該特定時間點保存變量的值。 然后,當您想在以后恢復該功能并使用在您的應用程序中已更改的變量的值時...您可以使用閉包原始功能中的一個功能來實現

This gets easier, I promise.

我保證,這會變得更容易。

So, when the heck would you use a closure?

那么,什么時候使用閉包呢?

Let’s say you are building an interactive map of tourist landmarks in New York City using the Google Maps API. You have an array with a bunch of map markers that you want to add to the map- the Statue of Liberty, Empire State Building, Coney Island, you name it. You want to add all these markers to the map, but you also want to add a click event to each marker. When you click the marker, you want to show dynamic information about that marker, including live weather data.

假設您正在使用Google Maps API構建紐約市旅游地標的交互式地圖。 您有一個包含一堆要添加到地圖的地圖標記的數組-自由女神像,帝國大廈,康尼島,您將其命名。 您想將所有這些標記添加到地圖,但也想將click事件添加到每個標記。 單擊標記時,您要顯示有關該標記的動態信息,包括實時天氣數據。

var touristPlaces= […];
for(var i=0; i< touristPlaces.length; i++){  var marker= touristPlaces[i];  $(marker).click(function(){    showToolTip(i)  });}

Here’s the issue- if you write it like this, it will not work. The ‘for’ loop will finish before the callback in the click event can register the appropriate i value. You need to capture this intermediate point so you can call the function later with the appropriate i.

這是問題所在-如果您這樣編寫, 它將無法正常工作 。 在單擊事件中的回調可以注冊適當的i值之前,“ for”循環將完成。 您需要捕獲此中間點,以便稍后可以使用適當的i調用該函數。

What do you need to know first?

您首先需要了解什么?

  1. Variable scoping

    可變范圍
  2. The concept of callbacks (I wrote a guide on this too!)

    回調的概念(我也為此寫過指南 !)

If you are looking for a technical explanation of closures, the guide on MDN is probably the best.

如果您正在尋找有關閉包的技術說明, 則有關MDN的指南可能是最好的。

關閉與郵寄包裹的過程相同。 (Closures have the same process as mailing a package.)

Let’s look at some basic code that uses a closure to mail a package.

讓我們看一些使用閉包郵寄包裹的基本代碼。

The addressPackage() function is a closure! It can be called at any time after the packBox function has been called. It also has access to the variables and arguments from the time when packBox() was originally called.

addressPackage()函數是一個閉包 ! 可以在調用packBox函數后隨時調用它。 從最初調用packBox()以來,它還可以訪問變量和參數。

Notice how the console.log output does not show until lines 14 and 15? This is extremely important. If you ran this code after line 11, you would simply see ‘Put jersey in box’. There would be no error, but the closure, addressPackage(), would not run at that point.

請注意console.log輸出如何直到第14和15行才顯示? 這是非常重要的。 如果您在第11行之后運行此代碼,則只會看到“將球衣放入框中”。 不會有錯誤,但是閉包addressPackage()不會在此時運行。

When you are mailing a package, you would probably agree that your job is not done until the package is filled and the address is written. Likewise, the packBox() function waits until the closure has also been called. Let’s go through this line-by-line.

當您郵寄包裹時,您可能會同意在填滿包裹并寫出地址之前不會完成您的工作。 同樣,packBox()函數將等待,直到還調用了閉包。 讓我們逐行進行介紹。

Line 11: You create the variable brotherGift, which is an instance of the packBox() function. You are sending a jersey to your brother.

第11行:創建變量brotherGift,它是packBox()函數的一個實例 。 您正在向您的兄弟發送球衣。

Line 3: Your code logs a statement about the jersey.

第3行:您的代碼記錄了有關球衣的聲明。

Line 8: The packBox() function returns… another function? Huh?

第8行: packBox()函數返回…另一個函數? ??

Let’s stop here, and assume that line 13 has not run yet. Here is what is happening: The packBox() function will not return the “ready to send” line until you also call the addressPackage() function with an argument. Just like there are two steps to sending a package: first, filling it, and second, addressing it. Your package is worthless if it has no contents or it does not have an address! That being said, you do not necessarily need to address the package directly after you fill the contents. You can wait a few days before addressing it. You might need to go to your computer to look up the address. You might be waiting for your brother to officially change his address!

讓我們在這里停止,并假設第13行尚未運行。 這是發生的情況:在您還使用參數調用addressPackage()函數之前,packBox()函數將不會返回“準備發送”行。 就像發送包裹有兩個步驟一樣:第一步,填充包裹,第二步,處理包裹。 如果您的包裹沒有物品或沒有地址,那將毫無價值! 話雖如此,填寫內容后,您不一定需要直接處理包裹。 您可以等待幾天,然后再解決。 您可能需要轉到計算機以查找地址。 您可能正在等待您的兄弟正式更改他的地址!

Regardless, if you do not address the package immediately, this does not mean that the package will somehow magically empty itself. The contents will still be there when you return to address it! So, any time we call brotherGift, the first argument, jersey, will still be available.

無論如何,如果您不立即處理該軟件包,這并不意味著該軟件包將以某種方式神奇地清空其自身。 返回地址時內容仍然在那里! 因此,無論何時我們將其稱為brotherGift,第一個參數jersey仍然可用。

…Waiting…Waiting…Now let’s run line 13.

…正在等待…正在等待…現在運行第13行。

Line 13: Alright, let’s finish off this instance! You are ready to add the address, so you call brotherGift and offer the address as an argument. Remember from line 11, brotherGift is an instance of packBox with the ‘jersey’ argument. So when you call it, you are offering another argument, which will then be sent to the closure: addressPackage();

第13行:好的,讓我們結束這個實例 ! 您已經準備好添加地址,因此可以調用brotherGift并提供該地址作為參數。 請記住,從第11行開始,brotherGift是帶有'jersey'參數的packBox的實例 。 因此,當您調用它時,您將提供另一個參數,然后將其發送到閉包:addressPackage();

Line 3: The console.log will show since we are now running the code from line 13.

第3行:由于我們現在正在運行第13行的代碼,因此將顯示console.log。

Line 4: We now offer the second argument to addressPackage();

第4行:現在,我們為addressPackage()提供第二個參數;

Line 6: addressPackage logs a statement related to the address argument.

第6行: addressPackage記錄與address參數相關的語句。

Line 8: The return statement can fire for this instance.

第8行: return語句可以為此實例觸發。

Again, closures allow us to have this intermediate instance where one argument has been filled, but brotherGift is left unfulfilled until we add the second argument. If we wanted to do this in one line, we would write: packBox(‘jersey’)(‘123 Main Street, Anywhere USA 01234’);

再次,閉包允許我們擁有一個已填充一個參數的中間實例,但是在添加第二個參數之前,不執行brotherGift。 如果要在一行中執行此操作 ,則應編寫:packBox('jersey')('123 Main Street,Anywhere USA 01234');

再舉一個例子 (One More Example)

Let’s say that you wanted to send a gift to each member of your family. You might pack each box before adding the addresses to each. This is what that looks like in code.

假設您想發送禮物給家人。 您可以在將每個地址添加到每個地址之前打包它們。 這就是代碼中的樣子。

Another magical feature of closures! Each instance is able to use the correct gift item with the correct address, even thought we run the function with 4 separate gift/address pairs. In a traditional function, there is no concept of memory. You would need to explicitly restate the original gifts in lines 6–15 if you wanted to use a traditional function.

關閉的另一個神奇功能! 每個實例都能夠使用具有正確地址的正確禮物項目,即使我們以4個單獨的禮物/地址對運行該功能也是如此。 在傳統功能中,沒有內存的概念。 如果要使用傳統功能,則需要在第6-15行中明確聲明原始禮物。

您將在哪里使用 (Where You Will Use This)

You will frequently encounter closures in Node.js. If you are just interested in the front-end, think back to our original example. If you want to write a function that considers user input at two separate stages of your app, you may want to consider a closure!

您將經常在Node.js中遇到閉包。 如果您只是對前端感興趣,請回想一下我們最初的示例。 如果要編寫一個在應用程序的兩個單獨階段考慮用戶輸入的函數,則可能需要考慮閉包!

Did you enjoy this guide? Or are you having trouble with another JavaScript topic? Give it a heart and let me know in the comments!

您喜歡本指南嗎? 還是您在處理另一個JavaScript主題時遇到麻煩? 放心,讓我在評論中知道!

Looking for other JavaScript concepts explained? Check out these past articles in the series.

尋找其他解釋過JavaScript概念嗎? 查看本系列中的這些過去的文章。

JavaScript Promises Explained By Gambling At A Casino

在賭場賭博解釋JavaScript承諾

Model-View-Controller (MVC) Explained Through Ordering Drinks At The Bar

通過在酒吧訂購飲料來解釋模型視圖控制器(MVC)

JavaScript Callbacks Explained Using Minions

使用Minions解釋JavaScript回調

翻譯自: https://www.freecodecamp.org/news/javascript-closures-explained-by-mailing-a-package-4f23e9885039/

javascript閉包

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/396139.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/396139.shtml
英文地址,請注明出處:http://en.pswp.cn/news/396139.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

linux 加入ad 用ssh,使用samba驗證AD用戶,允許AD用戶登錄到linux

使用samba驗證AD用戶&#xff0c;允許AD用戶登錄到linux2007年06月26日 星期二 14:101、先把samba加入到AD域中2、在smb.conf中添加一行&#xff0c;讓登錄進來的用戶使用bashtemplate shell /bin/bash3、運行authconfig&#xff0c;在驗證中選擇 使用smb和kerberos,winbind驗…

體會日子

體會日子 開通了博客園&#xff0c; 日子要記錄一下。 以后要好好上進了。 posted on 2016-05-01 23:16 體會日子 閱讀(...) 評論(...) 編輯 收藏 轉載于:https://www.cnblogs.com/yukunshi/p/5451485.html

selenium 難定位元素、時間插件

關于frame: 1. 如果網頁存在iframe的話&#xff0c;傳統的定位有時候找不到元素&#xff0c;需要切換frame&#xff1b; # 切換到leftFrame定位“測井設計” driver.switch_to_frame("leftFrame") driver.find_element_by_link_text(u"設計").click() # 切…

python kmeans聚類 對二維坐標點聚類_Kmeans均值聚類算法原理以及Python如何實現

第一步.隨機生成質心由于這是一個無監督學習的算法&#xff0c;因此我們首先在一個二維的坐標軸下隨機給定一堆點&#xff0c;并隨即給定兩個質心&#xff0c;我們這個算法的目的就是將這一堆點根據它們自身的坐標特征分為兩類&#xff0c;因此選取了兩個質心&#xff0c;什么時…

HDU 2544最短路dijkstra模板題

最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 33657 Accepted Submission(s): 14617Problem Description在每年的校賽里&#xff0c;全部進入決賽的同學都會獲得一件非常美麗的t-shirt。可是每當我們…

我為期一個月的GitHub的經驗教訓

by JS由JS 我為期一個月的GitHub的經驗教訓 (Lessons from my month-long GitHub commit streak) “I want to learn JavaScript. Like, really learn it. Like, truly understand it.” — me in November 2016“我想學習JavaScript。 喜歡&#xff0c;真正地學習它。 喜歡&a…

安裝itunes需要管理員身份_ITUNES無法安裝,提示沒有權限如何解決?

展開全部注意機器一定要登陸管理員系統&#xff0c;如果現在不是&#xff0c;可以注62616964757a686964616fe78988e69d8331333365646263銷&#xff0c;切換一下用戶。還有計算機不要有漏洞&#xff0c;如果有的話修復一下。打開開始運行,輸入regedit,點擊確認打開注冊表編輯器,…

vs2012新建項目產生的問題

當用vs新建web項目時遇到 只需下載一個vs2012的更新插件 http://download.microsoft.com/download/A/0/2/A02C37E0-77F7-448A-BD5C-F66AB1F78DBC/VS11-KB3002339.exe 點擊安裝更新即可. 轉載于:https://www.cnblogs.com/GreenLeaves/p/5452073.html

zoj4062 Plants vs. Zombies 二分+模擬(貪心的思維)

題目傳送門 題目大意&#xff1a;有n個植物排成一排&#xff0c;標號為1-n&#xff0c;每株植物有自己的生長速度ai&#xff0c;每對植物澆一次水&#xff0c;該株植物就長高ai&#xff0c;現在機器人從第0個格子出發&#xff0c;每次走一步&#xff0c;不能停留&#xff0c;每…

MyBatis注解模式批量insert方法

2019獨角獸企業重金招聘Python工程師標準>>> 方法一:script標簽方式 Insert("<script>insert into xxx (channelId,siteId) " "values " "<foreach collection\"list\" item\"item\" index\"index\&quo…

尚硅谷學費有住宿么_我在12個小時的住宿期間了解到的硅谷知識

尚硅谷學費有住宿么by Sahil Khoja由Sahil Khoja 我在12個小時的住宿期間了解到的硅谷知識 (What I learned about Silicon Valley during my 12 hour stay) #1 Unless you’re a designer or a developer, the billboards are pure gibberish.&#xff03;1除非您是設計師或開…

以下屬于linux文件系統認為的文件是,信息安全技術題庫:在Linux系統中,圖形文件、數據文件、文檔文件等都屬于()。...

相關題目與解析Linux中圖像文件屬于()。A、文本文件B、連接文件C、特殊文件D、二進制文件主要用于Linux系統中進程間相互傳遞數據。A&#xff0e;FIFO文件B&#xff0e;設備文件C&#xff0e;鏈接文件D&#xff0e;目錄文件關于Linux文件組織方式的說法中&#xff0c;(32)是錯誤…

關于eclipse中文注釋亂碼的問題

今天打開了一個以前的android項目&#xff0c;發現中文注釋都成亂碼啦&#xff01;&#xff01;&#xff01; 后來在網上找了一會解決方法&#xff0c;知道了中文的編碼大體是兩種&#xff1a;GBK(漢字內碼擴展規范)和UTF-8(8-bit Unicode Transformation Format)。 因此問題的…

園林系統優秀黨員推薦材料_園林綠化公司黨員先進個人事跡材料

第1頁共5頁三一文庫(www.31doc.com)〔園林綠化公司黨員先進個人事跡材料〕我于年月踏出校門來到建設公司。初到公司&#xff0c;我被分配到分公司卉豐園林綠化公司工作。我努力學習公司各項規章制度和相關業務知識&#xff0c;多了解樹木、綠化的有關情況。在此期間&#xff0c…

python入門(5)使用文件編輯器編寫代碼并保存執行

python入門&#xff08;5&#xff09;使用文件編輯器編寫代碼并保存執行 兩款文本編輯器&#xff1a; 一個是Sublime Text&#xff0c;免費使用&#xff0c;但是不付費會彈出提示框&#xff1a; 一個是Notepad&#xff0c;免費使用&#xff0c;有中文界面&#xff1a; 請注意&…

js 獲取時間戳的方法

(new Date()).valueOf()1541569364658(new Date()).getTime()1541569372623Number(new Date())1541569386622 // 2019年1月23日補充 *除以1000得到的是Unix時間戳 // Math.floor(new Date().getTime() / 1000), // 當天// (new Date(new Date().setHours(0, 0, 0, 0)) / 1000) …

agpl限制了開源_不要限制您的開源項目的潛力

agpl限制了開源by Julien Danjou通過朱利安丹喬(Julien Danjou) 不要限制您的開源項目的潛力 (Don’t limit your open source project’s potential) During the OpenStack summit a few weeks ago, I had the chance to talk to some people about my experience on running…

linux 批量同步,多主機目錄到備份服務器批量同步腳本

為了方便同步多個主機的目錄到備份服務器&#xff0c;寫了如下腳本&#xff1a;#!/usr/bin/perluse strict;use File::Spec;use File::Basename;use File::Path;#設定存儲路徑my $storedir"/backup/";while(){chomp;my ($host,$s_path)split /\t/;my $project_namefi…

交流電的有效值rms值_交流電路的功率三角因數原來是這樣理解的

點擊“電工電氣學習”關注即可免費訂閱&#xff01;電工學習網&#xff1a;www.diangon.com關注電工學習網官方微信公眾號“電工電氣學習”&#xff0c;收獲更多經驗知識。交流電路中消耗的電能可以用直角三角形的三個邊來表示&#xff0c;通常稱為功率三角形我們在關于交流電路…

CSS3酷炫樣式集合

1、30種炫酷CSS鼠標滑過按鈕特效 2、CSS 變量實現炫酷鼠標懸浮效果 3、基于CSS3和jQuery實現跟隨鼠標方位的Hover特效 4、css3金屬質感登錄表單 4、CSS3動態下拉菜單 5、CSS3鼠標懸浮特效 轉載于:https://www.cnblogs.com/mankii/p/9922981.html