博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js closure ,copy from stackoverflow
阅读量:5780 次
发布时间:2019-06-18

本文共 2517 字,大约阅读时间需要 8 分钟。

Whenever you see the function keyword within another function, the inner function has access to variables in the outer function.

function foo(x) {  var tmp = 3;  function bar(y) {    alert(x + y + (++tmp)); // will alert 16  }  bar(10);}foo(2);

This will always alert 16, because bar can access the x which was defined as an argument to foo, and it can also access tmp from foo.

That is a closure. A function doesn't have to return in order to be called a closure. Simply accessing variables outside of your immediate lexical scope creates a closure.

function foo(x) {  var tmp = 3;  return function (y) {    alert(x + y + (++tmp)); // will also alert 16  }}var bar = foo(2); // bar is now a closure.bar(10);

The above function will also alert 16, because bar can still refer to x and tmp, even though it is no longer directly inside the scope.

However, since tmp is still hanging around inside bar's closure, it is also being incremented. It will be incremented each time you call bar.

The simplest example of a closure is this:

var a = 10;function test() {  console.log(a); // will output 10  console.log(b); // will output 6}var b = 6;test();

When a JavaScript function is invoked, a new execution context is created. Together with the function arguments and the parent object, this execution context also receives all the variables declared outside of it (in the above example, both 'a' and 'b').

It is possible to create more than one closure function, either by returning a list of them or by setting them to global variables. All of these will refer to the same x and the same tmp, they don't make their own copies.

Here the number x is a literal number. As with other literals in JavaScript, when foo is called, the number x is copied into foo as its argument x.

On the other hand, JavaScript always uses references when dealing with objects. If say, you called foo with an object, the closure it returns will reference that original object!

function foo(x) {  var tmp = 3;  return function (y) {    alert(x + y + tmp);    x.memb = x.memb ? x.memb + 1 : 1;    alert(x.memb);  }}var age = new Number(2);var bar = foo(age); // bar is now a closure referencing age.bar(10);

As expected, each call to bar(10) will increment x.memb. What might not be expected, is that x is simply referring to the same object as the age variable! After a couple of calls to bar, age.memb will be 2! This referencing is the basis for memory leaks with HTML objects.

本文转自  陈小龙哈   51CTO博客,原文链接:http://blog.51cto.com/chenxiaolong/1687571

转载地址:http://rruyx.baihongyu.com/

你可能感兴趣的文章
打造敏捷工作空间
查看>>
管理微服务中的数据
查看>>
Firefox Developer Edition推出弹出窗口调试
查看>>
Java社区领袖介绍平台支持选项
查看>>
专访季虎:如何突破瓶颈构建高质量风控系统?
查看>>
Universal Windows Platform(UWP)应用的窗口特性
查看>>
Istio 1.1 版本发布,性能和可用性提升
查看>>
公有云运维福利 | 开源监控小工具:Open-Falcon插件cloud-mon
查看>>
从程序员到架构师的最佳技术成长之路
查看>>
入门解读:小白也能看懂的容器和虚拟机介绍
查看>>
Eclipse基金会发布Eclipse Photon IDE
查看>>
亚马逊增加了对Aurora的跨区域和加密复制支持
查看>>
GitLab揭示DevOps价值和挑战的新调查研究
查看>>
IONIC 云端打包(在线编译)
查看>>
来自社区的Visual Studio Code使用体验和教程
查看>>
GraphQL与认证
查看>>
又拍云沈志华:如何打造一款安全的App
查看>>
Netflix推出Hollow,处理内存数据集的Java库
查看>>
GitHub发布桌面版但不包括Linux
查看>>
Jessica Kerr:高绩效团队简史
查看>>