Javascript - difference between namespace vs. closure?

前端 未结 3 445
无人共我
无人共我 2021-02-02 03:34

In Javascript, what\'s the difference between a namespace and a closure? They seem very similar to me.

EDIT

Specifically, this article discusses namesp

相关标签:
3条回答
  • 2021-02-02 03:51

    A namespace is typically a method of putting all your global variables as properties under one master global variable, thus only adding one new truly top-level global variable. It prevents pollution of the global namespace and reduces the chance of conflict with other global variables.

    And example of a namespace:

    var YUI = {};
    YUI.one = function(sel) {...};
    YUI.two = function(sel) {...};
    YUI.three = function(sel) {...};
    

    There is one new item in the top-level global namespace YUI, but there are multiple globally accessible items via the YUI namespace object.

    A closure is a function block that lasts beyond the normal finish of the execution of the function because of lasting references to internal parts of the function.

    function doSometing() {
        var x = 10;
        setTimer(function() {
            // this gets called after doSomething() has finished executing
            // but because of the function closure, the variables 
            // inside of the parent scope like x are still accessible
            x += 10;
        }, 1000);
    }
    
    0 讨论(0)
  • 2021-02-02 04:03

    A namespace is essentially an Object with no interesting properties that you shove stuff into so you don't have a bunch of variables with similar and/or conflicting names running around your scope. So, for example, something like

    MyNS = {}
    MyNS.x = 2
    MyNS.func = function() { return 7; }
    

    A closure is when a function 'retains' the values of variables that are not defined in it, even though those variables have gone out of scope. Take the following:

    function makeCounter() { 
       var x = 0;
       return function() { return x++; }
    }
    

    If I let c = makeCounter() and then repeatedly call c(), I'll get 0, 1, 2, 3, .... This is because the scope of the inner anonymous function that makeCounter defines 'closes' over x, so it has a reference to it even though x is out of scope.

    Notably, if I then do d = makeCounter(), d() will start counting from 0. This is because c and d get different instances of x.

    0 讨论(0)
  • 2021-02-02 04:05

    From http://jibbering.com/faq/notes/closures/:

    A closure is formed by returning a function object that was created within an execution context of a function call from that function call and assigning a reference to that inner function to a property of another object. Or by directly assigning a reference to such a function object to, for example, a global variable, a property of a globally accessible object or an object passed by reference as an argument to the outer function call.

    Namespaces are just a convention, objects created to avoid cluttering the global scope with variables.

    0 讨论(0)
提交回复
热议问题