feklee
feklee

Reputation: 7695

Accessing variable in outer scope?

(function () {
    var x = 1;
    return {
        f: function (x) {
            alert(x);
        }
    };
}()).f(2);

Suppose I don't want to rename either variable. There is no way to, from within f, access the variable x, which was declared first - right?

Upvotes: 9

Views: 8992

Answers (5)

Oleg Neumyvakin
Oleg Neumyvakin

Reputation: 10282

Aware of implicit async calls which make you think that you can't access of variable from outer scope:

result = {}
jQuery.ajax({ // it is a async call!
    url: "/some/url",
    success: function(data) {
        result = JSON.parse(data);
    }
});

return result; // Result will be still {} because function returns before request has done.

Upvotes: 0

Tomalak
Tomalak

Reputation: 338218

There is no way to, from within f, access the variable x, which was declared first

No, there is not. The inner scope x hides the outer scope x.

var closure = (function () {
    var local = {};
    local.x = 1;
    return {
        f: function (x) {
            alert(x || local.x);
        }
    };
}());

closure.f(2);  // alerts "2"
closure.f();   // alerts "1"

You can't have an inner variable called "local", of course. ;-)

Upvotes: 1

Fenton
Fenton

Reputation: 250922

This allows you to use both x (1) and x (2) at the same time.

(function () {
    var x = 1;
    return {
        f: function (x) {
            alert(x); // paramter (=2)
            alert(this.x); // scoped variable (=1)
        },
        x:x
    };
}()).f(2);

Upvotes: 5

Quentin
Quentin

Reputation: 943561

Correct. Because you have a different x in function (x), any attempt to access x will get that one (the nearest scope). It blocks access to any x in a wider scope.

Upvotes: 11

tcooc
tcooc

Reputation: 21209

You could return the variable with the function:

(function () {
    var x = 1;
    return {
        f: function () {
            alert(this.x);
        },
        x:x
    };
}()).f();

Upvotes: 1

Related Questions