0

Can we reuse var name without overwriting its details in JavaScript.Taking below example of augmented module pattern

  <script>

var Module = (function () {

  // Module object 
  var module = {},
    privateVariable = "Hello World";

  function privateMethod() {
    // ...
  }

  module.publicProperty = "Foobar";
  module.publicMethod = function () {
    //console.log( privateVariable );
    return privateVariable;
  };
  return module;

})();

 //Defining same name MODULE var again
var Module = (function (my) {
      var newvar = 999;
    my.anotherMethod = function () {
         return newvar;
    };

    return my;
}(Module));



alert(Module.publicMethod()); 
//How this upper MODULE property is accessible ? Should be  hide by 
//next same name MODULE var?**
alert(Module.anotherMethod());

</script>

Above code is running perfectly fine and adding one more anotherMethod under MODULE but how it is still accessing initial module property. Defining var with same name (MODULE) shoudn't overwrite (remove) the upper module.

user3236118
  • 23
  • 1
  • 4

1 Answers1

0

Defining var with same name (MODULE) shoudn't overwrite (remove) the upper module.

Yes, it should.

The global scope (or the function if you are inside a function) is scanned for var statements. Since you have two identical ones, the second one is ignored. MODULE is created in the global scope.

Then you run a function and then assign its return value to the MODULE.

Then you run another function, passing the current value of MODULE as an argument to it, then you assign the return value to MODULE.

A reference to the object created at var my = {} still exists in the scope of the second function, but the only reference to it from the global scope has been overwritten.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Do you mean to say ,since I have passed entire MODULE (first function object) to second function object ,so all properties copied to second function object ( second MODULE) and first MODULE var does not make sense now – user3236118 Jan 25 '14 at 21:24
  • No. The variables `my`, `MODULE` and `MODULE` (in your original code) all contain *references* to objects. A JavaScript variable will never contain the object itself, only a reference to it. – Quentin Jan 25 '14 at 21:26
  • Ok but I am changing the reference of object and it is behaving like reference append by maintain old references .Now MODULE is referencing both function (upper and lower). How? – user3236118 Jan 25 '14 at 21:34
  • `MODULE` *never* references any function. You only assign the return values of function calls to it. – Quentin Jan 25 '14 at 21:34
  • So the return value of second function contains the first function property as it is passed in, this (second function).But my question is,if I mention MODULE in my alerts (in code) means second MODULE var?Also could you please clarify more on never references any function as you said earlier A JavaScript variable will never contain the object itself, only a reference to it.Thanks – user3236118 Jan 25 '14 at 21:48
  • The return value of the second function is (a reference to) the object you originally assigned to `my` in the first function which you then returned. It's just had more properties assigned to it. When you refer to `MODULE` it's value will be whatever the last bit of code that ran that assigned something to it set it to be. – Quentin Jan 25 '14 at 21:50