Returning a function from a function, complete with variables from an external scope, is called a closure.

The entire contents of one of these inner functions will still be available outside the outermost function.

Example:

function buildTicket(allRides, passRides, pick){
if(passRides[0]==pick){
var pass = passRides.shift();
return function(){alert("Quick! You've got a Fast Pass to "+ pass +"!");};
}else{
for(vari = 0; i < allRides.length; i++){
if(allRides[i][0]==pick){
return function(){alert("A ticket is printing for "+ pick + "!\n" +
"Your wait time is about "+ allRides[i][1]+ "minutes.");};
}
}
}
}

Simaple example:

//Simple example
function testClosure(){
var x = 4;
return x;
} //We call testClosure() --> 4
//But we call x --> undefined function textClosure(){
var x = 4; //For closeX(), x just like a g_v
function closeX(){
return x;
};
return closeX;
}
var closureX = textClosure();
closureX(); //We get 4, because, x is binded with closeX()

What Closure can be used in patical?

--> A closure can make the creation of very similar funcitons ultra-efficient.

See the image below, buildCoveTicketMarker function return function.

According the usage, we pass the fuction with different param, then assign to different variables:

getSubmarineTicket, getBattleshipTicket, getGiantSeagullTicket

They are very similar function, but different usage!

Then we have a loot the what function return:

All of three return the same function, and the transport, name variable still the same, becasue we didn't pass the param yet.

------------------------Ex------------------------------

var hidden = mystery();
var result = hidden(3); function mystery ( ){
var secret = 6;
function mystery2 ( multiplier ) {
multiplier *= 3;
return secret * multiplier;
}
return mystery2;
}
// alert(result); --> 54 var hidden = mystery(4);
var result = hidden(2); function mystery ( input ){
var secret = 5;
function mystery2 ( multiplier ) {
multiplier *= input;
return secret * multiplier;
}
return mystery2;
}
// alert(result); --> 40 var hidden = mystery(3);
var jumble = mystery3(hidden);
var result = jumble(2); function mystery ( input ){
var secret = 4;
input+=2;
function mystery2 ( multiplier ) {
multiplier *= input;
return secret * multiplier;
}
return mystery2;
}
function mystery3 ( param ){
function mystery4 ( bonus ){
return param(6) + bonus;
}
return mystery4;
}
// alert(result); --> 122 function warningMaker( obstacle ){
return function () {
alert("Beware! There have been " + obstacle + " sightings in the Cove today!");
};
} var icebergAlert = warningMaker('iceberg');
icebergAlert(); function warningMaker( obstacle ){
return function ( number, location ) {
alert("Beware! There have been " +
obstacle +
" sightings in the Cove today!\n" +
number +
" " +
obstacle +
"(s) spotted at the " +
location +
"!"
);
};
}
var killerPenguinAlert = warningMaker("killer penguin");
var polarBearAlert = warningMaker("polar bear");
var icebergAlert = warningMaker("iceberg");
var flashBlizzardAlert = warningMaker("flash blizzard");
var snowYetiAlert = warningMaker("snow yeti"); killerPenguinAlert(6, "Ice Caves");
snowYetiAlert(1, "Blizzard Beach");

[Javascript] Closure Cove, 1的更多相关文章

  1. [Javascript] Closure Cove, Common mistake

    They’ve got a problem with their existing code, which tries to use a closure. Check it out: function ...

  2. 如何理解javascript closure ?

    接触过javascript的人应该听过闭包(closure),有一种观点认为是闭包赋予了javascript的强大能力,也赋予了它具备OOP的特征.既然javascript closure如此重要,那 ...

  3. [Javascript]Clouse Cove, 2 ,Modifying Bound Values After Closure

    function buildCoveTicketMarker(transport){ var passengerNumber = 0; return function(name){ passenger ...

  4. javascript closure 闭包 事件绑定

    先来一个基本的例子 <!-- 实现一段脚本,使得点击对应链接alert出相应的编号 --> <meta http-equiv="Content-Type" con ...

  5. About javascript closure

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures?redirectlocale=en-US&redi ...

  6. js闭包之初步理解( JavaScript closure)

    闭包一直是js中一个比较难于理解的东西,而平时用途又非常多,因此不得不对闭包进行必要的理解,现在来说说我对js闭包的理解. 要理解闭包,肯定是要先了解js的一个重要特性, 回想一下,那就是函数作用域, ...

  7. javascript closure

    http://www.jibbering.com/faq/notes/closures/ http://hi.baidu.com/bluedream_119/item/938dcd082b1e1880 ...

  8. 深入理解JavaScript闭包【译】

    在<高级程序设计>中,对于闭包一直没有很好的解释,在stackoverflow上翻出了一篇很老的<JavaScript closure for dummies>(2016)~ ...

  9. Javascript闭包——懂不懂由你,反正我是懂了

    摘要:“如果你不能向一个六岁的孩子解释清楚,那么其实你自己根本就没弄懂.”好吧,我试着向一个27岁的朋友就是JS闭包(JavaScript closure)却彻底失败了. 越来越觉得国内没有教书育人的 ...

随机推荐

  1. 递归与分治策略之循环赛日程表Java实现

    递归与分治策略之循环赛日程表 一.问题描述 设有n=2^k个运动员要进行网球循环赛.现要设计一个满足以下要求的比赛日程表: (1)每个选手必须与其他n-1个选手各赛一次: (2)每个选手一天只能参赛一 ...

  2. php 安全模式限制函数

    表 42-2. 安全模式限制函数 函数名 限制 dbmopen() 检查被操作的文件或目录是否与正在执行的脚本有相同的 UID(所有者). dbase_open() 检查被操作的文件或目录是否与正在执 ...

  3. vijos p1882 智力题

    题意: 清晨, Alice与Bob在石阶上玩砖块.他们每人都有属于自己的一堆砖块.每人的砖块都由N列组成且N是奇数.Alice的第i列砖块有m[i]个.而Bob的第i列砖块有s[i]个. 他们想建造城 ...

  4. hdu 2110 基础母函数

    题意:退出本身并不麻烦,麻烦的是,退出的人需要取走相应比例(1/3)金额的资产.假设公司此时一共有n种价值的资产,每种价值的资产数量已知,请帮助心烦意乱的XHD夫妇计算一共有多少种分割资产的方法.   ...

  5. Markdown中如何插入视频 > iframe?

    关于Markdown中如何插入视频这一问题   网上众说纷纭,一直也没找到一个确切的答案,想来也是,这些东西毕竟还不算成熟.各种以前提供过的方法现在来讲,可能在更新或是关闭大潮中又没了   而且,Ma ...

  6. 你的C/C++程序为什么无法运行?揭秘Segmentation fault (2)

    什么让你对C/C++如此恐惧? 本篇将继续上一篇来讨论段错误(Segmentation fault). 上一篇: 你的C/C++程序为什么无法运行?揭秘Segmentation fault(1) 追溯 ...

  7. lanmp安装一(centos+apache+nginx+mysql+php=lanmp地址下载)

    背景 centos7 官网地址https://www.centos.org/download/ 下载选择DVD版进入(也就是标准版,系统齐全) 点击任何一个国家的下载链接 下载地址  http://m ...

  8. POJ 3468 A Simple Problem with Integers (splay tree入门)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 47944   ...

  9. Clever Little Box 电缆组件 USB A 插座 至 USB B 插头

    http://china.rs-online.com/web/p/usb-cable-assemblies/7244156/ 产品详细信息 USB3.0适配器 superspeed USB将提供10x ...

  10. Windows程序的打包,部署(vs项目打包vs2013)---ShinePans

    Windows 应用程序在开发完毕之后,怎样将程序打包并制作成安装程序在客户机上部署 是每一个windows应用程序开发完毕之后都必须面对的问题. 学习目标:                    部 ...