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.mzph.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