Closures

Closures are one of the most powerful features of JavaScript. JavaScript allows for the nesting of functions and grants the inner function full access to all the variables and functions defined inside the outer function (and all other variables and functions that the outer function has access to).

闭包是JavaScript中最强大的特性之一。JavaScript允许函数嵌套,并且内部函数可以访问定义在外部函数中的所有变量和函数,以及外部函数能访问的所有变量和函数。

However, the outer function does not have access to the variables and functions defined inside the inner function. This provides a sort of encapsulation for the variables of the inner function.

然而,外部函数却不能访问定义在内部函数的变量和函数。这给内部函数的变量提供了一定的安全性

Also, since the inner function has access to the scope of the outer function, the variables and functions defined in the outer function will live longer than the duration of the outer function execution, if the inner function manages to survive beyond the life of the outer function. A closure is created when the inner function is somehow made available to any scope outside the outer function.

此外,由于内部函数可以访问外部函数的作用域,因此当内部函数生存周期大于外部函数时,外部函数中定义的变量和函数的生命周期将比内部函数执行时间长。当内部函数以某一种方式被任何外部函数作用域访问时,一个闭包就产生了

var pet = function(name) { // The outer function defines a variable called "name"
var getName = function() {
return name; // The inner function has access to the "name" variable of the outer function
}
return getName; // Return the inner function, there by exposing it to outer scopes
}
myPet = pet('Vivie'); console.log(myPet()); // Returns "Vivie"

It can be much more complex than the code above. An object containing methods for manipulating the inner variables of the outer function can be returned.

它可能比上面的代码复杂的多。可以返回包含操作外部函数内部变量的方法的对象

var createPet = function (name) {
var sex; return {
setName: function(newName) {
name = newName;
}, getName: function() {
return name;
}, setSex: function(newSex) {
if (typeof newSex === 'string' && (newSex.toLowerCase() === 'male' || newSex.toLowerCase() === 'female')) {
sex = newSex;
}
}, getSex: function () {
return sex;
}
}
} var pet = createPet('Vivie');
console.log(pet.getName()); pet.setName('Oliver');
pet.setSex('male');
console.log(pet.getSex());
console.log(pet.getName());

Closures Basic的更多相关文章

  1. How do JavaScript closures work?

    Like the old Albert Einstein said: If you can't explain it to a six-year-old, you really don't under ...

  2. Atitit HTTP 认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结

    Atitit HTTP认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结 1.1. 最广泛使用的是基本验证 ( ...

  3. Basic Tutorials of Redis(9) -First Edition RedisHelper

    After learning the basic opreation of Redis,we should take some time to summarize the usage. And I w ...

  4. Basic Tutorials of Redis(8) -Transaction

    Data play an important part in our project,how can we ensure correctness of the data and prevent the ...

  5. Basic Tutorials of Redis(7) -Publish and Subscribe

    This post is mainly about the publishment and subscription in Redis.I think you may subscribe some o ...

  6. Basic Tutorials of Redis(6) - List

    Redis's List is different from C#'s List,but similar with C#'s LinkedList.Sometimes I confuse with t ...

  7. Basic Tutorials of Redis(5) - Sorted Set

    The last post is mainly about the unsorted set,in this post I will show you the sorted set playing a ...

  8. Basic Tutorials of Redis(4) -Set

    This post will introduce you to some usages of Set in Redis.The Set is a unordered set,it means that ...

  9. Basic Tutorials of Redis(3) -Hash

    When you first saw the name of Hash,what do you think?HashSet,HashTable or other data structs of C#? ...

随机推荐

  1. 截取nginx日志

    截取nginx日志 sed -n '/24\/Feb\/2017:11:00:00/,/24\/Feb\/2017:12:00:00/p' yunying_api.wanglibao.com.acce ...

  2. 《Android的设计与实现:卷I》——第1章 1.2.2动态视角的体系结构

    1.2.2 动态视角的体系结构静态的体系结构是从横向分层的角度诠释Android是什么.如果静态的体系结构不足以让读者理解Android的运行机制,我们可以看看Google工程师Sans Serif是 ...

  3. 程序员还在用360,腾讯电脑管家清理注册表,清理垃圾?只能说你太low

    首先明明电脑上,就有清理垃圾和无用注册表的功能,只是我么缺少发现美的眼睛. 为什么不用360,腾讯全家桶. 那玩意固然香,而且真香,但是后台占用率太高,作为一个有洁癖的我,实在是不想看到自己右下角多一 ...

  4. 基于UDP的客户端和服务器端的代码设计

    实验平台 linux 实验内容 编写UDP服务器和客户端程序,客户端发送消息,服务器接收消息,并打印客户端的IP地址和端口号. 实验原理 UDP是无需连接的通信,其主要实现过程如下: 同样,我们可以按 ...

  5. 安卓commandlinetools-win-6200805_latest配置

    JDK:1.8.0_251 系统:win10 64bit 问题1 官网下载commandlinetools,解压运行报错 解决方法 打开sdkmanager.bat,修改第17行为set DEFAUL ...

  6. 【SpringBoot 基础系列】实现一个自定义配置加载器(应用篇)

    [SpringBoot 基础系列]实现一个自定义配置加载器(应用篇) Spring 中提供了@Value注解,用来绑定配置,可以实现从配置文件中,读取对应的配置并赋值给成员变量:某些时候,我们的配置可 ...

  7. 【Linux基础总结】Linux基本环境

    Linux基本环境 对Linux的基础认识 虚拟机进入终端: [root@hadoop-senior Desktop] # 用户名 主机名 所在目录名称 #:表示当前用户属于root用户,超级管理员用 ...

  8. 74LS 系列 名称解释

    摘自:http://blog.sina.com.cn/s/blog_502ffce50100j9db.html -------------------------------------------- ...

  9. [hihoCoder1236 Scores 2015BeijingOnline]简单粗暴的分块+简单粗暴的bitset

    题意:50000个5维向量,50000次询问每一维都不大于某一向量的向量个数,强制在线. 思路:做完这题才知道bitset效率这么高,自己本地测试了下1s可以操作1010个bit,orz简单粗暴 令S ...

  10. Linux登录shell和非登录(交互式shell)环境变量配置

    使用Jenkins执行shell脚本的时候, 碰到command not found. 比如java mvn, 这些环境变量配置在/etc/profile 中, 但jenkins执行的时候并没有加载. ...