1. About Javascript variable declaration
In Javascript, declare a variable
var a=1;
or you can directly declare these two expressions:
a=1;
There is a difference,
One is a local variable of the current scope, and the other is a global variable of the current scope;
The special thing about the Javascript language is that global variables can be read directly inside the function.
var n=999;
function f1() {
alert(n);
}
f1(); // 999
On the other hand, local variables within the function cannot be read naturally from outside the function.
function f1(){
var n=999 ;
}
alert(n); // error
2.
Javascript variable scope chain
var x='000 ';
document.writeln(x); //Get '000 '
a();
function a(){
var x='aaa ';
function b(){
document.writeln(x); //undefined
var x='bbb ';
document.writeln(x); //bbb
}
b();
document.writeln(x); //aaa
}
//The result is: 000 undefined bbb aaa
Principle:
When a variable is used, first find it from the function block (explained by the calling object in the authoritative guide),
If you find If not, search from the upper-level function block until it is found.
If the definition is not found until the top-level code (referring to the position of var x='000 ';), the code will report an undefined error.
1. In the order of execution, output x '000 ' (this is no problem);
2. Then execute a()
3. Execute b() in a()
4 .b() needs to output x. There is a definition of x in the function body (scope), but it has not been assigned a value, so the output is undefined; (Key point!)
5. Then output x, x has been assigned a value, so bbb is output. ;
6. Finally output aaa;
Understanding the above principles, let’s look at the following example
var x = "global";
function f() {
var x='f1';
function f2(){
x= 'f2' ;//I'm a little confused here, GLOBAL's global"
}
f2();
alert(x) //return "f2"
}
f();
alert(x); //return "global ", has not been reassigned to: f2
//The results pop up respectively: f2 global f2 global
Explanation:
First execute f2() in f(),
f2() creates a scope for the internal function, so x='f2' modifies the x value in f(), not the global x.
alert(x); is 'f2', alert(window. x) is 'global'.
Then execute alert(x); the scope of x is global, which is 'global'
3. Suggestions for novices
1. Reduce Global variables (solution: encapsulate variables into objects)
Quote: “Attributing all your messy footprints on the global world to one person can significantly reduce the risk of interoperability with other applications, gadgets or JS libraries. Possibility of conflict. ”
– Douglas Crockford
var name = 'Jeffrey';
var lastName = 'Way';
function doSomething() {...}
console.log(name); // Jeffrey -- or window.name
Better way of writing
var DudeNameSpace = {
name : 'Jeffrey',
lastName : 'Way',
doSomething : function() {...}
}
console.log(DudeNameSpace.name); // Jeffrey
Note how we dramatically put the "messy footprints" under the "DudeNameSpace" object;
2. A long list of variable declarations? Don’t write so many vars, use commas
var someItem = 'some string';
var anotherItem = 'another string';
var oneMoreItem = 'one more string';
더 나은 작성 방법
var someItem = '어떤 문자열',
anotherItem = '또 다른 문자열',
oneMoreItem = '하나 더 문자열'
자명합니다. 이것이 코드 속도를 높일지는 모르겠지만, 코드가 훨씬 더 깔끔해질 것입니다.