Scope of object properties & methods

Posted by Anish on Stack Overflow See other posts from Stack Overflow or by Anish
Published on 2011-07-01T07:25:55Z Indexed on 2011/07/01 8:22 UTC
Read the original article Hit count: 341

Filed under:
|
|
|

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()

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about object