Net-informations.com
SiteMap  | About    

Source file in a function block

Javascript in a browser has a couple of effective scopes: function scope and global scope . If a variable isn't in function scope, it's in global scope. And global variables are generally make more troubles in JavaScript , so this is a construct to keep a library's variables to itself.

Parameters and variables defined in a function are not visible outside of the function, and that a variable defined anywhere within a function is visible everywhere within the function. It basically seals the code inside the function so that other libraries don't interfere with it. It's similar to creating a namespace in compiled languages. It creates a closure around the content of the file which basically creates a private namespace which helps in avoiding any name clashes between different javascript libraries and modules. This way if you define a "print()" function, it won't clash with some other file's "print()" function.

Main purposes :

  1. Avoid clash with other methods/libraries.
  2. Avoid Global scope, make it local scope.
  3. Make debugging faster (local scope).
  4. Avoid overriding already existing variables.
  5. Use easily referenceable alias for a global variables.









net-informations.com (C) 2018    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.