惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - Adam.Zhao

ejs 模板使用方法 我使用的开源组件汇总,以备学习使用 了不起的Node.js--之五 TCP连接 Windows7下Java运行时环境搭建 了不起的Node.js--之四 - Adam.Zhao 了不起的Node.js--之三 了不起的Node.js--之二 - Adam.Zhao 了不起的Node.js--之一 - Adam.Zhao 请大家有需要购物的到我的网站上吧。 AngularJS应用骨架 最近在研究google的angularjs 好久没有来写博客了 c#二进制文件数据转换base64字符串文本代码 ActionFilterAttribute做切面编程的Url的格式化例子 学习content-type 网站性能优化之服务端(一) 艺龙旅行网招聘了 关于项目管理的几点建议 HP CQ35 Windows7声卡驱动安装不上问题 - Adam.Zhao
简单示例理解神闭包
Adam.Zhao · 2015-10-07 · via 博客园 - Adam.Zhao
闭包,一个近乎神话的概念,对于那些有一点javascript使用经验但从未真正理解闭包概念的人来说,理解闭包可以看做是某种意义上的重生,但是需要付出非常多的努力才能理解这个概念,今天我们通过简单示例来理解一下神闭包。
// 最简单的闭包示例
function foo(){
    var a = 2;

    function bar(){
        console.log(a);
    }

    return bar;
}

var baz = foo();
baz(); // 2   ----朋友,这就是闭包效果

// 传递函数的闭包效果
function foo(){
    var a = 2;

    function baz(){
        console.log(a);
    }

    bar(baz);
}

function bar(fn){
    fn();     // 妈呀快看啊,这就是闭包效果
}

// 传递函数当然也可以是间接的
var fn;

function foo(){
    var a = 2;

    function baz(){
        console.log(a);
    }

    fn = baz;
}

function bar(){
    fn();   // 妈呀快看啊,这就是闭包效果
}

foo();

bar(); // 2

// 处处都是闭包,延时器方法来演示闭包
function wait(message){

    setTimeout(function timer(){
        console.log(message);
    }, 1000);
}

wait("hello,closure!");

//将一个内部函数(名为timer)传递给setTimeout(..)。timer具有涵盖wait(..)作用域的闭包。因此还保有对变量message的引用。
//wait(..)执行1000毫秒后,它的内部作用域并不会消失,timer函数依然保有wait(..)作用域的闭包。
//深入到引擎的内部原理中,内置的工具函数setTimeout(..)持有对一个参数的引用,这个参数也许叫做fn或者func,或者其他类似的名字。
//引擎会调用这个函数,在例子中就是内部的timer函数,而词法作用域在这个过程中保持完整。
//这就是闭包。

// 循环和闭包
// 要说明闭包,for循环是最常见的例子。
for(var i=1;i<=5;i++){
    setTimeout(function timer(){
        console.log(i);
    }, i*1000);
}

// 正常情况下,我们对这段代码行为的预期是分别输出数字1~5,每秒一次,每次一个。
// 但实际上,这段代码在运行时会以每秒一次的频率输出五次6.

// 仔细想想原因,关键在于延迟函数的回调会在循环结束时才执行,而当循环结束后,i的值已经变为了6
// 我们可以看看最终运行的顺序是下面这样的
setTimeout(function timer(){
    console.log(6);
}, 1*1000);
setTimeout(function timer(){
    console.log(6);
}, 2*1000);
setTimeout(function timer(){
    console.log(6);
}, 3*1000);
setTimeout(function timer(){
    console.log(6);
}, 4*1000);
setTimeout(function timer(){
    console.log(6);
}, 5*1000);

// 想要实现我们要的效果,实际上需要给回调函数一个独立的i的副本,这样i的值变化,就不会影响回调函数执行中得副本值了
for(var i=1;i<=5;i++){
    (function(){
        var j = i;
        setTimeout(function timer(){
            console.log(j);
        }, j*1000);
    })();
}

// 或者如下写法也可以
for(var i=1;i<=5;i++){
    (function(j){
        setTimeout(function timer(){
            console.log(j);
        }, j*1000);
    })(i);
}

// 重返块作用域,let关键字可以实现,但因为目前对浏览器的支持不够,支持javascript1.8版本以上的浏览器才可以,所以我们建议非特殊情况,不使用这个关键字
for(var i=1;i<=5;i++){
    let j=i;
    setTimeout(function timer(){
        console.log(j);
    }, j*1000);
}
//或者更简单写法
for(let i=1;i<=5;i++){
    setTimeout(function timer(){
        console.log(i);
    }, i*1000);
}

//正确的效果的运行顺序如下:
setTimeout(function timer(){
    console.log(1);
}, 1*1000);
setTimeout(function timer(){
    console.log(2);
}, 2*1000);
setTimeout(function timer(){
    console.log(3);
}, 3*1000);
setTimeout(function timer(){
    console.log(4);
}, 4*1000);
setTimeout(function timer(){
    console.log(5);
}, 5*1000);
通过这几个简单示例,对闭包有了初步了解,闭包处处都在,在乎我们如何观察和理解
更多信息可以看看我的博文,并下载相关资料