Learning JavaScript(0)_Concepts

作用域,嵌套函数和闭包

<script type="text/javascript">

    function foo(){
        var a = 10;
        function bar(){
            a *= 2;
        }
        bar();
        return a;
    }

</script>

在这个示例中,a定义在函数foo中,但函数bar可以访问它,因为bar也定义在foo中。当bar在foo中北调用时它可以访问a,但是如果bar是在foo外部被调用呢?

<script type="text/javascript">

    function foo(){
        var a = 10;
        function bar(){
            a *= 2;
            return a;
        }
        return bar;
    }

    var baz = foo();
    baz();  // return 20
    baz();  // return 40
    baz();  // return 80

    var blat = foo();
    blat();  // return 20
</script>

在上述的代码中,所返回的对bar函数的引用被赋予变量baz,函数bar现在是在foo外部被调用,但它依然能够访问a。这是因为JavaScript中的作用域是词法性的,函数式运行在定义它们的作用域中(本例是foo内部的作用域),而不是运行在调用它们的作用域中。只要bar被定义在foo中,它就能访问在foo中定义的所有变量,即使foo的执行已经结束。

这就是闭包的一个例子,在foo返回后,它的作用域被保存下来,但只有它返回的那个函数能够访问这个作用域。在前边的例子中,baz和blat各自拥有这个作用域以及a的一个副本,而且只有它们自己能够对其进行修改。返回一个内嵌函数式创建闭包最常用的手段,其他的手段还有...?

Learning JavaScript(0)_Concepts

时间: 2024-12-02 09:36:57

Learning JavaScript(0)_Concepts的相关文章

Learning JavaScript Design Patterns -- A book by Addy Osmani

Learning JavaScript Design Patterns A book by Addy Osmani Volume 1.6.2 Tweet Copyright © Addy Osmani 2015. Learning JavaScript Design Patterns is released under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 unported license. It

Survey Results for Rebecca Murpheys Learning JavaScript Survey

时间 2016-01-27 05:40:46  Raymond Camden's Blog 原文  http://www.raymondcamden.com/2016/01/25/survey-results-learning-javascript/ 主题 JavaScript A few weeks back, Rebecca Murphey tweeted out a link to a survey she was running involving how people learn Ja

Learning JavaScript Design Patterns The Observer Pattern

The Observer Pattern The Observer is a design pattern where an object (known as a subject) maintains a list of objects depending on it (observers), automatically notifying them of any changes to state. When a subject needs to notify observers about s

Learning JavaScript Design Patterns The Module Pattern

The Module Pattern Modules Modules are an integral piece of any robust application's architecture and typically help in keeping the units of code for a project both cleanly separated and organized. In JavaScript, there are several options for impleme

Learning JavaScript Design Patterns The Singleton Pattern

The Singleton Pattern The Singleton pattern is thus known because it restricts instantiation of a class to a single object. Classically, the Singleton pattern can be implemented by creating a class with a method that creates a new instance of the cla

自写小函数处理 javascript 0.3*0.2 浮点类型相乘问题

const reg = /^([0-9]+)\.([0-9]*)$/; // 判断是不是浮点数 const isFloat = function(number){ return reg.test(number); } // 去除小数点转为整数 const floatToInt = function(head,tail){//head:String tail:String // head和tail都是字符串 Number("005")可以去零 let result = head + Nu

Learning JavaScript Design Patterns The Constructor Pattern

In classical object-oriented programming languages, a constructor is a special method used to initialize a newly created object once memory has been allocated for it. In JavaScript, as almost everything is an object, we're most often interested in ob

Machine Learning Techniques -0

开学前还有一段时间,正好差不多可以follow台大Hsuan-Tien Lin老师Machine Learning Techniques这门课: 不过只输入信息而不输出效率太低,所以建个博客记录一下.

coffeescript 1.8.0 documents

CoffeeScript is a little language that compiles into JavaScript. Underneath that awkward Java-esque patina, JavaScript has always had a gorgeous heart. CoffeeScript is an attempt to expose the good parts of JavaScript in a simple way. The golden rule