- /***************************************
- * The result is to print out the global
- ****************************************/
- var a = ' global ';
- (Test () {
- Write (a);
- })();
- /***************************************
- * The result is to print out the undefined
- ****************************************/
- var a = ' global ';
- (Test () {
- Write (a);
- var a = ' local ';
- })();
- /***************************************
- * The result is to print out the local global
- ****************************************/
- var a = ' global ';
- (Test () {
- var a = ' local ';
- Write (a + ' \ n ');
- })();
- Write (a + ' \ n ');
As can be seen from the above example, the scope of JS is a function body. When a function body defines a local variable with the same name as a global variable, the global variable is overwritten by the local variable.
This article links http://www.cxybl.com/html/wyzz/JavaScript_Ajax/20130318/37230.html