Accessing Variables in another scope

I found this : but i don’T get it. I need the values of variables in another function. I cannot pass em as arguments. The function needs to be called without.

Consider using namespaces:

(function() {
    var local_var = 'foo';
    global_var = 'bar'; // this.global_var and window.global_var also work

    function local_function() {}
    global_function = function() {};
})();

Both local_function and global_function have access to all local and global variables.

Edit: Another common pattern:

var ns = (function() {
    // local stuff
    function foo() {}
    function bar() {}
    function baz() {} // this one stays invisible

    // stuff visible in namespace object
    return {
        foo : foo,
        bar : bar
    };
})();

The returned properties can now be accessed via the namespace object, e.g. ns.foo, while still retaining access to local definitions.

That’s what i need to do
Function a(){
var 1
var 2
var 3
}

function b(){
value of var 1 of function a
and so on}

simply not possible.

:frowning:

For all that you need to know about closure, and what you can do with them, the following excellent closure is a mine of useful information.

thx. thats really some value information. But for now the web storage api works perfect :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.