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#? ...
随机推荐
- JavaWEB开发时FCKeditor类似office界面的ajax框架,加入后就能做界面类似office,能进行简单的文本编辑操作+配置手册...
2019独角兽企业重金招聘Python工程师标准>>> FCKeditor是一款功能强大的开源在线文本编辑器(DHTML editor),它使你在web上可以使用类似微软Word 的 ...
- Centos 6.5升级gcc : 源码安装 + rpm安装
1. 前言 采用Centos 6.5默认的gcc版本为4.4.7,不支持c++ 11,需要升级: 首先想到用yum命令:执行yum update gcc-c++或yum update g++ 显示没有 ...
- 【mybatis】IF判断的坑
http://cheng-xinwei.iteye.com/blog/2008200 最近在项目使用mybatis中碰到个问题 <if test="type=='y'"> ...
- Coursera课程笔记----计算导论与C语言基础----Week 3
存储程序式计算机 冯·诺伊曼式计算机 "关于EDVAC的报告草案" 组成:控制器(协调工作).运算器(算数&逻辑运算).存储器(存储操作信息和中间结果).输入设备.输出设备 ...
- 流媒体与实时计算,Netflix公司Druid应用实践
Netflix(Nasdaq NFLX),也就是网飞公司,成立于1997年,是一家在线影片[租赁]提供商,主要提供Netflix超大数量的[DVD]并免费递送,总部位于美国加利福尼亚州洛斯盖图.199 ...
- Linux高性能服务器技术总结
文章目录 1 服务器简介 2 I/O复用技术 2.1 循环方式 2.2 select 方式 2.3 poll方式 2.4 epoll 方式 3 多线程方式 4 CPU多核并行计算 5 深度分析内核性能 ...
- [poj1741 Tree]树上点分治
题意:给一个N个节点的带权树,求长度小于等于K的路径条数 思路:选取一个点作为根root,假设f(root)是当前树的答案,那么答案来源于两部分: (1)路径不经过root,那么就是完全在子树内,这部 ...
- [codeforces-542-C]YY?
链接:http://codeforces.com/problemset/problem/542/C 题意:对一个函数f(x),定义域[1,n], 令f(k,x) = f(f(f(f...f(x)))) ...
- aui移动端UI库
aui 简介 aui 是一套基于原生javascript开发的移动端UI库,包含常用js方法.字符校验.dialog提示弹窗.侧滑菜单.时间选择器.多级联动.聊天UI.项目常用模板...... 特点 ...
- 三个方法生成python的exe文件
背景:用的python3.8 方法一:用cmd 输入[pip3 install pyinstaller] 上一条指令报错 事实上,在python3.8版本时,输入pip也会显示是无法支持的语句,需要用 ...