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. 每天认识几个HTTP 响应码

    HTTP 响应状态代码指示特定 HTTP 请求是否已成功完成. 1xx信息响应 1xx 的响应结果表明接收的请求正在处理 2xx成功响应 2XX 的响应结果表明请求被正常处理了 3xx重定向 3xx ...

  2. 微软开放 Build 2020 免费注册

    微软已经开放 Build 2020 线上开发者会议注册,https://mybuild.microsoft.com/.Build 2020 会议将于 5 月 19 日至 20 日召开,核心内容都是围绕 ...

  3. Eclipse Mac OS 安装 Subversion插件subclipse 缺失JavaHL解决方案

    安装 SVN 插件 subclipse 时可能遇到问题 subclipse 安装完成后,当我们选择使用 的时候还是会提示:javaHL not available, SVN接口选择 client:选择 ...

  4. 详解Linux 安装 JDK、Tomcat 和 MySQL(图文并茂)

    https://www.jb51.net/article/120984.htm

  5. 图论--SCC缩点--Tarjan

    // Tarjan算法求有向图强连通分量并缩点 /*强连通缩点与双连通缩点大同小异,也就是说将强连通分支缩成一个点之后,没有强连通,成为有向无环图,在对图进行题目的操作.*/ #include< ...

  6. 2020.2.27——STL初步

    注:本文主要针对STL中的常用的操作进行总结 目录: 1.swap 2.sort 3.reverse 4.min,max(比较简单,暂且略过) 5._gcd 6.lower_bound &&a ...

  7. spark系列-3、缓存、共享变量

    一.persist  和 unpersist 1.1.persist() : 用来设置RDD的存储级别 存储级别 意义 MEMORY_ONLY 将RDD作为反序列化的的对象存储JVM中.如果RDD不能 ...

  8. Python基础02 变量

    Python中的变量有两个特点: 1. 无需声明 a = 1 2. 不与类型绑定 a = 1 a = 'hello world' 变量名只是内存中具体对象的一个引用(reference). 对于 a ...

  9. weak_ptr

    #include <iostream> #include <memory> using namespace std; int main(int argc, char **arg ...

  10. 一篇博客带你轻松应对java面试中的多线程与高并发

    1. Java线程的创建方式 (1)继承thread类 thread类本质是实现了runnable接口的一个实例,代表线程的一个实例.启动线程的方式start方法.start是一个本地方法,执行后,执 ...