Closures Basic
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的更多相关文章
- 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 ...
- Atitit HTTP 认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结
Atitit HTTP认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结 1.1. 最广泛使用的是基本验证 ( ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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#? ...
随机推荐
- 【Linux常见命令】tar命令
[独立命令,只能用其中一个,和别的命令连用]这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个. -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末 ...
- Facebook发布神经蛋分离法,可从嘈杂环境中提取音视频
分离混合分布是机器学习和信号处理的长期挑战,而Facebook近日提出的新方法似乎可以有效解决这一难题. 人类天生善于分离个别声音和视觉效果,例如在拥挤的鸡尾酒会上听到别人的声音,或者在动物穿过灌木丛 ...
- angularJS中$http.get( ).success( )报错原因及解决方案
一.问题描述: 电脑安装的angular1.6.7版本,项目中使用了$http.get( ).success( ),控制台报错: $http.get(...).success is not a fun ...
- 洛谷 2016 战略游戏(树形DP)
题目描述 Bob喜欢玩电脑游戏,特别是战略游戏.但是他经常无法找到快速玩过游戏的办法.现在他有个问题. 他要建立一个古城堡,城堡中的路形成一棵树.他要在这棵树的结点上放置最少数目的士兵,使得这些士兵能 ...
- Java——抽象类与接口的前世今生
该系列博文会告诉你如何从入门到进阶,一步步地学习Java基础知识,并上手进行实战,接着了解每个Java知识点背后的实现原理,更完整地了解整个Java技术体系,形成自己的知识框架. 1.抽象类: 当编写 ...
- 面试官:你说你懂动态代理,那你知道为什么JDK中的代理类都要继承Proxy吗?
之前我已经写过了关于动态代理的两篇文章,本来以为这块应该没啥问题,没想到今天又被难住了- 太难了!!! 之前文章的链接: 动态代理学习(一)自己动手模拟JDK动态代理. 动态代理学习(二)JDK动态代 ...
- Spring Cloud学习 之 Spring Cloud Hystrix(基础知识铺垫)
Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 文章目录 前述: 快速入门: 命令模式: RxJava: 前述: 在微服务架构中, ...
- 向Redis里存入数据
实现思路:1. 从Redis缓存获取URL统计网址清单2. 逐条拼凑SQL统计语句,暂时不能支持批量计算,因为按单个网址统计.3. 发送到HIVE JDBC执行SQL并等待返回结果4 ...
- 1020 Tree Traversals (25分)思路分析 + 满分代码
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...
- 盘点6个Kubernetes监视工具
导读:监控可帮助您确保Kubernetes应用程序平稳运行并排除可能出现的任何问题.Prometheus是一种流行的开源监视工具,许多公司都使用它来监视其IT基础结构.但是,还有许多其他监视工具可用. ...