4

I'd like to access a local variable globally (without changing the namespace or location of the variable). I read that I could use 'window.', but it's not working:

var myNameSpace = {
        window.localVar : 'foo'
    };

alert(window.localVar);  // Not working

JSFiddle

Can something like this work?

Also, I've read about the risks of global variables. If I'm nearly certain a variable's name isn't at risk to be reused, is it safe?

$myTotallyUniqueVarName
2
  • "but it's not working:" ... well, using window.lovalVar is syntax error at this position: but it's not working: SyntaxError: Unexpected token .`. Commented Feb 21, 2013 at 15:04
  • Re "Not working": In what way is it not working? Commented Oct 15, 2020 at 18:44

2 Answers 2

4
var myNameSpace = { };

myNameSpace.localVar = window.localVar = 'foo';

You can't access another object when defining the key from another object, like you tried. But you certainly can create multiple references/assignments to the same value, like shown above.

However, be aware that we assigning a primitive value in this case, that means, window.localVar and myNameSpace.localVar will hold their own unique value. Any change will not reflect on the other side.

Sign up to request clarification or add additional context in comments.

Comments

1

Every variable that is somehow related to the global object can be accessed globally. In browsers, the window variable represents the global object.

function a() {
  bvar = 3; // Accessible
  var cvar = 4; // Inaccessible
}

a();

alert(window.bvar === 3); // true
alert(window.cvar === 4); // false, cvar is undefined

// This refers here to the global object
alert(window.bvar === this.bvar); // true

4 Comments

thanks, that's helpful. so bvar is accessible because it's a key, or because it isn't a variable?
In JavaScript everything can be seen as an array. So, a function is an array with properties, and the global object can be seen as a array to. The function a() and bvar are both 'items' / properties on the global object. (every variable declared without the var prefix will leak into the global object.) so, window.a and window.bvar are accessable. cvar is declared inside a function and becomes a property of the functions' activation object'. Properties of activation object of functions are not accessable outside the function scope. Hope this helps, space is little here to explain this.
@Andries: I'd rather say everything is an object. Arrays are special types of objects.
@Felix: True, thats why i carefully expressed myself by saying 'can be seen as'. I wanted to stress the array-behavior of JavaScript like: window['cvar'] === window.cvar. But you also right. Maybe we should say, everything is an special type of an object.:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.