`
txf2004
  • 浏览: 6886509 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

JS closures

 
阅读更多


JS 闭包原理: functions are executed using the scope chain that was in effect when they were defined.(来自《JavaScript:The Definitive Guide》).

闭包形成也就是:如果一个函数返回另一个函数,而被返回函数又需要外层函数的变量时,不会立即释放这个变量,而是允许被返回的函数引用这些变量。支持这种机制的语言称为支持闭包机制,而这个内部函数连同其自由变量就形成了一个闭包

var scope = "global scope";

 function checkScope(){
    var scope = "local scope";
    function f() { return scope;}
    return f;
}
console.log(checkScope()());


当调用checkScope()();时,返回的是"local scope";也就是,调用的返回函数仍然使用的是局部变量。因为其定义时候,使用的就是局部变量,所以在返回调用时候,仍然持有对局部变量的引用。

将上面代码修改一下:

var scope = "global scope";

var c = {
    scope: "c scope"
}

 function checkScope(){
    var scope = "local scope";
    function f() { return this.scope;}
    return f;
}
console.log(checkScope()());
console.log(checkScope().call(c));  
console.log(checkScope().call(this));
打印出来的分别为:global scope c scope global scope

这里,改变了f()函数调用的变量。因而其不将再引用局部变量scope = "local scope";而是使用调用checkScope()这个函数的对象的scope变量。也就是f()没有引用局部变量时,其将像普通局部变量一样进行回收。








分享到:
评论

相关推荐

    secrets_of_javascript_closures.pdf

    secrets_of_javascript_closures.pdf

    javascript 闭包

    javascript closures leaning,闭包学习

    Advanced JavaScript (closures,prototype,inheritance)

    NULL 博文链接:https://butterflymacro.iteye.com/blog/1271789

    scope-chains-closures, Javascript作用域链和闭包 workshop.zip

    scope-chains-closures, Javascript作用域链和闭包 workshop 范围链和闭包 workshop正在启动$ npm install -g scope-chains-closures$ scope-chains-closures # or, shorter: sccjs使用箭头

    Programming JavaScript Applications

    , AMD, Asynchronous Operations, Callbacks, Promises and Deferreds, Code Quality, Function Polymorphism, Function Scope, Hoisting and Closures, Functional Programming and Stateless Functions, ...

    classes-and-closures

    完成closures.js和constructors.js内部的所有活动。使用它们各自的html文件来查看是否可以使所有测试通过。记住要经常提交并推送您的代码。祝你好运! 技巧和窍门 开发人员可以使用的最大工具之一是浏览器的内置调试...

    Mastering.JavaScript.1785281348

    Key Features - Write powerful code with the high-level functions that JavaScript offers - Test and debug issues with JavaScript...- Learn to build scalable server application in JavaScript using Node.js

    functional javascript

    You'll also get a thorough reference to the Underscore.js library and its idioms, including: Closures Applicative programming Laziness Immutability Higher-order functions Purity Combinators Currying ...

    JavaScript Functional Programming for JavaScript Developers (PDF, EPUB, MOBI)

    The first module Mastering JavaScript, stress on practical aspects of Javascript development like—Functions and Closures, Runtime debugging techniques, project layout, events and DOM processing, ...

    深入理解JavaScript系列

    深入理解JavaScript系列(16):闭包(Closures) 深入理解JavaScript系列(17):面向对象编程之一般理论 深入理解JavaScript系列(18):面向对象编程之ECMAScript实现 深入理解JavaScript系列(19):求值策略...

    closures-scope-js:javascript的关闭范围

    闭包-范围-js javascript的关闭封闭范围

    2017java源码-pycon2017-closures:PyconSK2017中的Python,Java,C#,JavaScript的幻灯

    js包含javascript / node.js中的示例 csharp包含C#、. NET(核心)中的示例 java包含Java 8中的示例 议程 简介,自下而上的解释 池-python,java,c#,javascript的用法 问题-函数列表示例(10,10,10 ... 10) ...

    Learning JavaScript Design Patterns 英文原版.js设计模式

    If you want to write ... If you’re familiar with concepts such as closures and prototypal inheritance, you’ll be able to determine why some patterns may be more suitable for your projects than others.

    深入理解JavaScript系列(.chm)

    深入理解JavaScript系列(16):闭包(Closures) 深入理解JavaScript系列(17):面向对象编程之一般理论 深入理解JavaScript系列(18):面向对象编程之ECMAScript实现 深入理解JavaScript系列(19):求值策略...

    JavaScript.The.Definitive.Guide

    This Fifth Edition is completely revised and expanded to cover JavaScript as it is used in today's Web 2.0 ...Classes, closures, persistence, Flash, and JavaScript embedded in Java applications

    JavaScript权威指南(第五版).chm

    Classes, closures, persistence, Flash, and JavaScript embedded in Java applications Part I explains the core JavaScript language in detail. If you are new to JavaScript, it will teach you the ...

    深入理解JavaScript系列.chm

    16.闭包(Closures) 17.面向对象编程之一般理论 18.面向对象编程之ECMAScript实现 19.求值策略 20.《你真懂JavaScript吗?》答案详解 21.S.O.L.I.D五大原则之接口隔离原则ISP 22.S.O.L.I.D五大原则之依赖倒置原则...

Global site tag (gtag.js) - Google Analytics