2

In the article Show love to the object literal, it's said:
When we have several scripts in a page, the global variables & functions will get overwritten if their name repeats.

One solution is to make the variables as properties & functions as methods of an object, and access them via the object name.

But will this prevent the issue of variables getting into the global namespace?

<script>
    var movie = {
        name: "a",
        trailer: function(){
           //code
        }
    };
</script>

In the above code which elements gets added to the global namespace?
a) Just the object name - movie
b) Object name as well as the properties and methods inside it – movie, movie.name, movie.trailer()

Anish
  • 1,164
  • 4
  • 15
  • 27

1 Answers1

3

movie will exist in the global namespace (in a browser: window). Within the movie-scope exist: name and trailer. You can see this if you try executing trailer from the global object (window.trailer() will result in ReferenceError: trailer is not defined). trailer can only be executed using movie.trailer() (or window.movie.trailer()).

Javascript has lexical scope, aka static scope, meaning:

  • an identifier at a particular place in a program always refers to the same variable location — where “always” means “every time that the containing expression is executed”, and that

  • the variable location to which it refers can be determined by static examination of the source code context in which that identifier appears, without having to consider the flow of execution through the program as a whole 1.

1 Source

KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • _Within the movie-scope exist: name and trailer._ But JavaScript has function scope. Here movie is an object. Then how does movie-scope come into play? – Anish Jul 01 '11 at 09:12
  • Javascript has *lexical* scope aka *static* scope. See my edit. – KooiInc Jul 01 '11 at 10:18
  • @Kooilnc: In the example in the link you provided, variable a is contained in a function. So it's environment is that function f1. But in the example in the question, name & trailer are contained in an object defined using the object literal notation, so it's not a function but an object. When we look from name & trailer to outside - there's no function, only the global space. So won't name & trailer be in the global namespace? – Anish Jul 01 '11 at 12:07
  • I think you are fixating on the `function` as the only possible environment for scope. Lexical scope concerns the *source code context in which that identifier appears*, so an Object can have it's own scope. – KooiInc Jul 01 '11 at 12:17
  • @Kooilnc: _an Object can have it's own scope_? In JavaScript, a new scope can be introduced only by using a function. http://stackoverflow.com/questions/500431/javascript-variable-scope/500438#500438 : _There is typically one global scope, and each function defined has its own nested scope._ http://www.digital-web.com/articles/scope_in_javascript/ : _Evaluating a function establishes a distinct execution context_ – Anish Jul 01 '11 at 12:41
  • http://en.wikipedia.org/wiki/JavaScript#Features : _JavaScript has function-level scoping. JavaScript 1.7, however, supports block-level scoping with the let keyword._ – Anish Jul 01 '11 at 12:49
  • *In a lexically scoped language, the scope of an **identifier** is fixed at compile time to **some region in the source code** containing the identifier's declaration. This means that an identifier is only accessible within that region (including procedures declared within it).* – KooiInc Jul 01 '11 at 13:18