- The global scope.
- The function scope.
- The class scope.
A magnitude x is different in function scope and in the global scope.
Source: http://enterprisejquery.com/2010/10/how ... ts-part-1/A best practice in C# is to limit the use of global variables. This doesn’t necessarily translate into a bad practice in JavaScript, but most C# developers are not aware of how easily it is to pollute the global namespace with needless variables and functions.
One way you can pollute the global namespace is to not declare your variables. In JavaScript, if you don’t declare your variables then they become globally available to the rest of the program, which is probably not what you wanted and generally a bad idea.
Name spaces are not formally implemented in JavaScripts, but there are informal methods to achive the same effect to avoid name clashes:
You can follow an approach like jQuery to allow inhabiting a custom "namespace"; but this is still a design-time issue.
Source: http://stackoverflow.com/questions/8815 ... eclaration
When one or more external libraries are included in your code, the risk of name conflicts ariese. The $() factory function that is an alias for jQuery() is an exaple of a potential name clash since the $() function is fairly wide spread in other libraries. A simple way to avoid this if you use jQuery is then naturallyt to use the longer unique name jQuery() in stead of $(). Another way is to use a callback and give the jQuery object itself to it like this:
Code: Select all
jQuery(document).ready(function($) {
// Here you can use $ as usual without an external library that also use the $() function.
});
Code: Select all
jQuery(function($) {
// Here you can use $ as usual without an external library that also use the $() function.
});
Related links:
http://www.dustindiaz.com/namespace-your-javascript/
http://stackoverflow.com/questions/8815 ... er-3588712