Scope of object properties & methods
- by Anish
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()