javascript - Scope of object properties & methods -


in article show love object literal, it's said:
when have several scripts in page, global variables & functions overwritten if name repeats.

one solution make variables properties & functions methods of object, , access them via object name.

but prevent issue of variables getting global namespace?

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

in above code elements gets added global namespace?
a) object name - movie
b) object name properties , methods inside – movie, movie.name, movie.trailer()

movie exist in global namespace (in browser: window). within movie-scope exist: name , trailer. can see if try executing trailer global object (window.trailer() result in referenceerror: trailer not defined). trailer can executed using movie.trailer() (or window.movie.trailer()).

javascript has lexical scope, aka static scope, meaning:

  • an identifier @ particular place in program refers same variable location — “always” means “every time containing expression executed”, , that

  • the variable location refers can determined static examination of source code context in identifier appears, without having consider flow of execution through program whole 1.

1 source


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -