closures
来源:互联网 发布:网络文明有哪些 编辑:程序博客网 时间:2024/06/15 12:09
Closures are functions that have access to variables from another function's scope. This is often accomplished by creating a function inside a function:
function createComparisonFunction(propertyName) {return function (obj1,obj2) {var value1=obj1[propertyName];var value2=obj2[propertyName];if(value1<value2){return -1;}else if(value1>value2){return 1;}else {return 0;}}}
The highlighted lines in this example are part of the inner function ( an anonymous function) that is accessing variable ( propertyName ) from the outer function. Even after the inner function has been returned and is being used elsewhere, it has access to that variable. This occurs because the inner function's scope chain includes the scope of createComparisonFunction().
The details of how scope chains are created and used are important for a good understanding of closures. When a function is called, an execution context is created , and its scope chain is created. The activation object for the function is initialized with values for arguments and any named arguments. The outer function's activation object is the second object in the scope chain. This process continues for all containing functions until the scope chain terminates with the global execution context.
Whenever a variable is accessed inside a function, the scope chain is search for a variable with the given name. Once the function has completed, the local activation object is destroyed, leaving only the global scope in memory. Closures , however , behave differently.
A function that is defined inside another function adds the containing function's activation object into its scope chain.
let compare=createComparisonFunction("name");let result=compare({name:"ni"},{name:"gre"});
When the anonymous function is returned from createComparisonFunction(), its scope chain has been initialized to contain the activation object from createComparisonFunction() and the global variable object. This gives the anonymous function access to all of the variable from createComparisonFunction(). Another interesting side effect is that the activation object from createComparisonFunction() can not be destroyed once the function finishes executing , because a reference still exist in the anonymous function's scope chain.
//create functionlet compare=createComparisonFunction("name");//call functonlet result=compare({name:"ni"},{name:"gre"});//dereference function - memory can now be reclaimedcompare=null;
- closures
- Closures
- closures
- Javascript Closures
- Javascript Closures
- Javascript Closures
- Javascript Closures
- JavaScript Closures
- Java Closures
- JS closures
- Java Closures
- Closures for Java
- Closures for Java
- Closures for Java
- Closures for Java
- Closures for Java
- Closures for Java
- Closures for Java
- 手写smali学习
- IsWordBreak
- 我有几张阿里云幸运券分享给你,用券购买或者升级阿里云相应产品会有特惠惊喜哦!把想要买的产品的幸运券都领走吧!快下手,马上就要抢光了。
- java基础(4)- 面向对象(1)
- 2017_12_04_周报
- closures
- 【第三方类库】Java Zookeeper 简概
- tensorflow学习笔记(1)-sess
- #step by step# Linux command line and explanation for NGS data
- Xamarin使用HttpClient类
- 第2章 使用JavaScript
- linux网络相关指令
- K8s安全总结
- 芬兰赫尔辛基:欢迎神秘的东方科技力量 阿里云:一起为创业者开挂