开发者

Inner(Private) Functions and Closures

Would some enlighten on the difference of Inner(Private) functions and Closures, as they appear very similar.

Inner Function:

function a(par开发者_如何学运维am) {
    function b(theinput) {
        return theinput * 2;
    };
    return 'The result is ' + b(param);
};

Closure

function f() {
    var b = "b";
    return function () {
        return b;
    }
}


Your second example explicitly returns a function object which maintains a link to the variables within the function which returned it, while your first example simply returns the result of invoking a private function. So the second example is of closure.

Here's a slightly modified version of your second example which better demonstrates a closure:

function f(){
    var a = "foo";

    // return an object containing two methods
    // which can access and modify the private "a"
    // variable even after the function has returned
    return {
        showA: function() {
          alert(a);  
        },
        changeA: function(str) {
            a = str;
        }  
    }   
}

var fun = f();
fun.showA(); // "foo"
fun.changeA("blahblah"); 
fun.showA(); // "blahblah"

Fiddle: http://jsfiddle.net/MKD6p/1

So as you can see, the stack frame has not been deallocated after the function has returned; the variables inside are kept alive and can be played with by means of a suitable interface.

Your first example's inner function does have access to its outer scope, and does return a value based on a variable declared in its outer function/scope/closure. But since a function or module has not been returned the variables local to the function are deallocated once the (outer) function has returned - so, while the the inner function does form a closure with its outer scope, it does not really serve as a productive example of what a closure is.


The Inner Function is a nested function call, while the Closure persists the life of the local variable past its normal scope.


MDN says:

You can nest a function within a function. The nested (inner) function is private to its containing (outer) function. It also forms a closure.

A closure is an expression (typically a function) that can have free variables together with an environment that binds those variables (that "closes" the expression).

Since a nested function is a closure, this means that a nested function can "inherit" the arguments and variables of its containing function. In other words, the inner function contains the scope of the outer function.

To summarize:

The inner function can be accessed only from statements in the outer function.

The inner function forms a closure: the inner function can use the arguments and variables of the outer function, while the outer function cannot use the arguments and variables of the inner function.


They're very similar in that they're both kinds of functions. An inner/private function is just one that has limited visibility. A closure is a function that captures the surrounding scope. Here's a code sample to illustrate:

function example() {
    var window = { 'location': null };
    var maybeARedirect = function() {
        window.location = "http://google.com";
    }
    maybeARedirect();
}

If the function() {} construct merely created an anonymous function, window would refer to global object and this would redirect the user to Google. But since it creates a closure, it will merely set the variable in the local window object and have no effect.

In most languages with anonymous functions, they at least have the ability to act as closures.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜