我也谈javascript闭包
1、什么是闭包呢?
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;
return function (y) {
alert(x + y + (++tmp)); // will also alert 16
}
}
var bar = foo(2); // bar is now a closure.
bar(10);
/*
* When a function is defined in another function and it
* has access to the outer function's context even after
* the outer function returns.
*
* An important concept to learn in JavaScript.
*/
function outerFunction(someNum) {
var someString = 'Hey!';
var content = document.getElementById('content');
function innerFunction() {
content.innerHTML = someNum + ': ' + someString;
content = null; // Internet Explorer memory leak for DOM reference
}
innerFunction();
}
outerFunction(1);
Two one sentence summaries:
- a closure is the local variables for a function — kept alive after the function has returned, or
- a closure is a stack-frame which is not deallocated when the function returns (as if a 'stack-frame' were malloc'ed instead of being on the stack!).
The following code returns a reference to a function:
function sayHello2(name) {
var text = 'Hello ' + name; // Local variable
var sayAlert = function() { alert(text); }
return sayAlert;
}
say2 = sayHello2('Bob');
say2();
Most JavaScript programmers will understand how a reference to a function is returned to a variable in the above code. If you don't, then you need to before you can learn closures. A C programmer would think of the function as returning a pointer to a function, and that the variables sayAlert and say2were each a pointer to a function.
There is a critical difference between a C pointer to a function and a JavaScript reference to a function. In JavaScript, you can think of a function reference variable as having both a pointer to a function as well as a hidden pointer to a closure.
The above code has a closure because the anonymous function function() { alert(text); } is declared inside another function, sayHello2() in this example. In JavaScript, if you use the functionkeyword inside another function, you are creating a closure.
In C, and most other common languages after a function returns, all the local variables are no longer accessible because the stack-frame is destroyed.
In JavaScript, if you declare a function within another function, then the local variables can remain accessible after returning from the function you called. This is demonstrated above, because we call the function say2() after we have returned from sayHello2(). Notice that the code that we call references the variable text, which was a local variable of the function sayHello2().
function() { alert(text); } // Output of say2.toString();
Click the button above to get JavaScript to print out the code for the anonymous function. You can see that the code refers to the variable text. The anonymous function can reference text which holds the value 'Bob' because the local variables of sayHello2() are kept in a closure.
The magic is that in JavaScript a function reference also has a secret reference to the closure it was created in — similar to how delegates are a method pointer plus a secret reference to an object.
我也谈javascript闭包的更多相关文章
- 我也谈javascript闭包的原理理解
参考原文:http://www.oschina.net/question/28_41112 前言:还是一篇入门文章.Javascript中有几个非常重要的语言特性——对象.原型继承.闭包.其中闭包 对 ...
- 也谈JavaScript闭包
闭包对于很多JavaScript初学者来说,都是比较难以理解的一个概念.其实,闭包并不是那么难以掌握的,理解闭包,只需要学会3个的基本事实. 首先我们来看第一个事实,JavaScript允许当前函数引 ...
- 浅谈Javascript闭包
垃圾回收器 我个人把闭包抽象的称之为”阻止垃圾回收器的函数”或者”有权访问另一个函数内部变量的函数"(当然这个是我个人的理解方式,每个人可能会有不同的理解方式),为什么这样说?这样说还得说说 ...
- 再谈JavaScript闭包及应用
.title-bar { width: 80%; height: 35px; padding-left: 35px; color: white; line-height: 35px; font-siz ...
- 浅谈JavaScript中的闭包
浅谈JavaScript中的闭包 在JavaScript中,闭包是指这样一个函数:它有权访问另一个函数作用域中的变量. 创建一个闭包的常用的方式:在一个函数内部创建另一个函数. 比如: functio ...
- 再谈JavaScript的closure--JavaScript 闭包
关于JavaScript的闭包,在我的博客上之前有一篇文章 https://www.cnblogs.com/wphl-27/p/8491327.html 今天看了几篇文章,感觉又有了一些更深的理解,特 ...
- 浅谈javascript函数节流
浅谈javascript函数节流 什么是函数节流? 函数节流简单的来说就是不想让该函数在很短的时间内连续被调用,比如我们最常见的是窗口缩放的时候,经常会执行一些其他的操作函数,比如发一个ajax请求等 ...
- 深入解析Javascript闭包
首先给个例子: function PfnOuter(){ var num=999; function PfnInner(){ alert(num); } return PfnInner; } var ...
- JavaScript ——闭包理解
昨天晚上听别人谈起闭包这个东西,虽然对js有一点了解但却丝毫没有印象,今天也没什么事就顺便研究了一下满足好奇宝宝.整合于网上的理解,记录一下. 一.闭包的作用域 要理解闭包,首先必须理解Javascr ...
随机推荐
- JS获取当前使用的浏览器名字以及版本号
JS获取当前使用的浏览器名字以及版本号 工作中需要通过JS去获取当前使用的浏览器的名字以及版本号,网上大堆资料都有一个关键词是 navigator.appName,但是这个方法获取的浏览器的名字只有两 ...
- scanf函数与scanf_s函数
ANSI C中没有scanf_s(),只有scanf(),scanf()在读取时不检查边界,所以可能会造成内存泄露.所以vc++2005/2008中提供了scanf_s(),在最新的VS2013中也提 ...
- 在Activity之间传递数据—简单数据/Bundle
1.首先要知道怎么通过一个Activity 打开另一个Activity.主页面为MainActivity,另一个页面为OtherActivity.MainActivity中的按钮Id为btnStart ...
- leetcode342合理运用位操作判断4的幂
Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Gi ...
- 第四十四节,subprocess系统命令模块
用python执行系命令的相关的模块和函数的功能均在 subprocess 模块中实现,并提供了丰富的功能 call()模块函数 功能:输入系统命令,直接执行命令,返回状态码,只能查看[有参] 使用方 ...
- 查看使用了那种shell
cat /etc/shells root@OpenWrt:/www/cgi-bin# cat /etc/shells/bin/ash
- 客户端 HttpUtils.java
package com.http.post; import java.io.ByteArrayOutputStream; import java.io.IOException; import java ...
- cdn是什么
CDN的全称是Content Delivery Network,即内容分发网络.其目的是通过在现有的Internet中增加一层新的网络架构, 将网站的内容发布到最接近用户的网络”边缘”,使用户可以就近 ...
- Google Gson的使用方法
用法1:从网络获取到json字符串之后,假如该字符串为data, Gson gson = new Gson(); HomeBean json = gson.fromJson(data, HomeBea ...
- MySQL源码安装(centos)
1.去MySQL官网下载源码包 地址:http://dev.mysql.com/downloads/mysql/ 下载完后需要检查文件的MD5,以确认是否从官网下载的原版本(以防被人篡改过该软件) 使 ...