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

推荐订阅源

Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
S
Security Affairs
P
Palo Alto Networks Blog
Webroot Blog
Webroot Blog
P
Privacy International News Feed
H
Hacker News: Front Page
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cybersecurity and Infrastructure Security Agency CISA
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
The Blog of Author Tim Ferriss
T
Threat Research - Cisco Blogs
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Schneier on Security
Schneier on Security
Jina AI
Jina AI
Simon Willison's Weblog
Simon Willison's Weblog
Scott Helme
Scott Helme
A
About on SuperTechFans
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Recorded Future
Recorded Future
F
Fortinet All Blogs
PCI Perspectives
PCI Perspectives
Google DeepMind News
Google DeepMind News
T
Troy Hunt's Blog
MyScale Blog
MyScale Blog
I
InfoQ
F
Full Disclosure
宝玉的分享
宝玉的分享
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Security Archives - TechRepublic
Security Archives - TechRepublic
Latest news
Latest news
W
WeLiveSecurity
月光博客
月光博客
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
N
Netflix TechBlog - Medium
量子位
P
Proofpoint News Feed
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
GbyAI
GbyAI
SecWiki News
SecWiki News
AI
AI
The Last Watchdog
The Last Watchdog
Hacker News - Newest:
Hacker News - Newest: "LLM"
G
GRAHAM CLULEY
N
News and Events Feed by Topic
K
Kaspersky official blog
V2EX - 技术
V2EX - 技术

博客园 - Adam.Zhao

ejs 模板使用方法 我使用的开源组件汇总,以备学习使用 了不起的Node.js--之五 TCP连接 Windows7下Java运行时环境搭建 了不起的Node.js--之四 了不起的Node.js--之三 了不起的Node.js--之二 了不起的Node.js--之一 请大家有需要购物的到我的网站上吧。 AngularJS应用骨架 最近在研究google的angularjs 好久没有来写博客了 c#二进制文件数据转换base64字符串文本代码 ActionFilterAttribute做切面编程的Url的格式化例子 学习content-type 网站性能优化之服务端(一) 艺龙旅行网招聘了 关于项目管理的几点建议 HP CQ35 Windows7声卡驱动安装不上问题
简单示例理解神闭包
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);
通过这几个简单示例,对闭包有了初步了解,闭包处处都在,在乎我们如何观察和理解
更多信息可以看看我的博文,并下载相关资料