0

I have a global variable MyGlobalVar and some code that looks like this:

var MyGlobalVar = null;

function PlayWithMyGlobal() {
    MyGlobalVar = new Object();
    .... adding properties to MyGlobalVar
    MoreFun(MyGlobal);
}

function MoreFun(TheVar) {
    is TheVar here a local or just a reference to the global?
} 

If I pass the global variable, am I still working with the global? Thanks.

frenchie
  • 51,731
  • 109
  • 304
  • 510
  • Nit: **variables are never passed**, instead, the *values* which *result from the evaluation of the expressions* are passed. An object (which is a type of value) -- which is not a variable -- is itself: while *internally* a "reference is passed", this is *not discussed in the specification* and it can be *entirely* described with [Call-By-Oject-Sharing](http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing) without adding in overloaded terms. –  Feb 25 '12 at 22:57

3 Answers3

5

If I pass the global variable, am I still working with the global? Thanks.

It depends whether variable you pass is an object or a primitive (number, string, boolean, undefined, null are primitives) value in the first place. Objects are passed by reference and primitives by value.

In your case, you are passing object:

MyGlobalVar = new Object();

And in JS, objects are passed by reference. So you are still working on that variable.

You can confirm it like this:

var MyGlobalVar = null;

function PlayWithMyGlobal() {
    MyGlobalVar = new Object();
    MoreFun(MyGlobalVar);
}

function MoreFun(TheVar) {
    MyGlobalVar.foo = 'I am foo'; // property created here
}

PlayWithMyGlobal();
console.log(MyGlobalVar.foo); // I am foo
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • *`bull`*? :p ("..., undefined, bull are primitives") – Rob W Feb 25 '12 at 22:37
  • 3
    *"In JS, objects are passed by reference"* -- [pedants would disagree](http://stackoverflow.com/a/9070366/201952), but that is how it is popularly understood. – josh3736 Feb 25 '12 at 22:40
  • @RobW: Ah typo, `null` actually :) – Sarfraz Feb 25 '12 at 22:40
  • I prefer [Call-By-Object-Sharing](http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing) -- no need to keep butchering the overloaded term that isn't even used in the specification. In *Java* the term "reference" is used and "call-by-value[-of-reference]" is how it works, *per JLS*; this, however (anything dealing with "references"), *does not* appear in the ECMAScript specification. It could, in theory, be implemented entirely without "references" (e.g. a JS engine in JS). –  Feb 25 '12 at 23:00
2

If the global variable is an object, then you're still working with the global variable. Otherwise, it's a copy.

As shown and annotated below, your variables point to the same global object.

var MyGlobalVar = null;

function PlayWithMyGlobal() {
    MyGlobalVar = new Object(); // <--- Object
    MoreFun(MyGlobalVar); // <--- Passing object reference
}

function MoreFun(TheVar) {
    TheVar.test = 'Did I modify global?';
    alert(TheVar === MyGlobalVar); // true
    alert(MyGlobalVar.test);       // "Did I modify global?"
} 
Rob W
  • 341,306
  • 83
  • 791
  • 678
1

Yes, you have a local reference to the same object that is referenced globally.

A simple test would be...

console.log(MyGlobalVar === TheVar); // should be true
  • This test is not sufficient. When `MyGlobalVar` and `TheVar` are primitive values, the identity comparison (`===`) still shows true, although they might have been defined in a different manner. – Rob W Feb 25 '12 at 22:35
  • @RobW: I'm answering the question, which states *"I have ... some code that looks like this*". Certainly you're not suggesting that an Object can be a primitive. –  Feb 25 '12 at 22:37
  • Certainly not, but by the way you phrased the answer, it seemed that the universal check to test whether a variable points to the same variable is by using `===`. When you rephrase that, I will delete my comments. – Rob W Feb 25 '12 at 22:39
  • @RobW: Nah, you can leave the comments. Your and Sarfraz's answers cover the primitives. +1 to both of yas... –  Feb 25 '12 at 22:40