3

Let say globalX is a global object variable. Lets define a function that takes that variable and inside another function takes same variable and changes the value inside it.

var globalX = [];

function a1(globalX){

a2(globalX);
console.log(globalX);
 //it shows "[]" not "[5,10]";

}

function a2(globalX){
globalX = [5,10];
}   

a1(globalX);

When I consoled that variable, console shows only parameter value in a1 not changed value in a2 function. How to reference changed value after calling a2?

Tarık Özgün Güner
  • 1,051
  • 10
  • 10
  • what type is globalx? also can you post the a2 function? – Nitsan Baleli Jun 02 '15 at 14:51
  • To refer to a global variable when using the same name for a local variable, you can generally use `window.globalvariable` – devnull69 Jun 02 '15 at 14:52
  • if globalx is an object, then it will be passed as a reference and therefore any change the method a2 makes to it's properties will be logged out in the next line. If globalx is a simple type (string/int), it will be passed by value. – HaukurHaf Jun 02 '15 at 14:55
  • 1
    possible duplicate of [Can I explicitly reference a JavaScript global variable that's been shadowed by a local with the same name?](http://stackoverflow.com/questions/12515513/can-i-explicitly-reference-a-javascript-global-variable-thats-been-shadowed-by) – Raymond Chen Jun 02 '15 at 14:58
  • Yup, this is a duplicate. – Venemo Jun 02 '15 at 15:03
  • @devnull69 That `window` thing only works if your global object is called `window` (ie. you're running in a browser) and you didn't also shadow `window` with a local variable. So that doesn't really answer the question. – Venemo Jun 02 '15 at 15:04
  • Well, if you name your local variables `window` you have some serious naming issues... – Robert Rossmann Jun 02 '15 at 15:28
  • I edited my question. You can see I did not use any local variables in a1 and a2 functions. Also you can see I dont use globalX directly in a1. When I remove parameter from a1 as "function a1(){a2(globalX);console.log(globalX);}" it worked as I want. And this is not a duplicate question, I did not use any local variable. – Tarık Özgün Güner Jun 02 '15 at 17:02

2 Answers2

1

In browser environments, you can refer to global variables by explicitly referencing the window object:

var reallyGlobalX = window.globalX

In node.js, there is no window object, but global is available:

var reallyGlobalX = global.globalX

Note that the variable must really be global - if it is just another variable from the outer scope, you cannot reach it once the variable has been shadowed.

Robert Rossmann
  • 11,931
  • 4
  • 42
  • 73
  • If we are not using var in any functions, we should use window as well? – Tarık Özgün Güner Jun 02 '15 at 17:08
  • You should use it only when you need it, i.e. a local variable shadows a global variable. Shadowing can happen by declaring a variable in your scope or naming a function's parameter as a global variable. Generally though, it is advised to simply rename your local variables or avoid using global scope. – Robert Rossmann Jun 02 '15 at 17:12
  • You mean if i defined "function a1(globalA1Par){...} function a2(globalA2Par){...}" is it solved? – Tarık Özgün Güner Jun 02 '15 at 17:23
  • You are right, i tested it and when changing name of parameters of functions it is worked. But even in this situation if we dont know the real name of the global variable, we cant access it if its value changed. Parameter holds only old value while called the main function. I think this is a bad design of Javascript. – Tarık Özgün Güner Jun 03 '15 at 06:27
-1

The short answer is NO.

The longer one is no, because JavaScript works with scopes. If you define the same variable again, it overrides the parent one. There are a few tricks to get it, but they all include reference to the parent object. Here are a few examples:

var start = 'start';
console.log(start); // 'start'

function test(start) {
    console.log(start); // 'test' - you got no reference to previous start
}
test('test');

Using globally (window):

start = 'start'; // global, without var, at document/window level
function test(start) {
    console.log(start); // test
    console.log(this.start); // start, because THIS is window/document
}
test('test');

BUT keep in mind, that this can be changed. Therefore there's no real proof that you will be able to get it.

Sometimes you will be able to do this:

function main() {
    this.start = 'start';

    return function(start) {
        console.log(start); // 'test'
        console.log(this.start); // 'start'
    }
}
var class = main();
class.test('test');

It all depends on your structure and if you wrap your things inside functions. Best practice is to just use different names. In some languages it's common to prefix your variables with _, like _start, which means that they are member variables (most top level as possible). Some people even suffix the parameter's variables (start_) so they know what's going on. I'm not fan of that, but still.

Andrey Popov
  • 7,362
  • 4
  • 38
  • 58