var x = "XX";
y ="xxx";
And so on. There is a problem with this. For example, in a line in the code, I want to use a declared variable x. As a result, due to typing or spelling errors, this variable is written as y. The result is equivalent to an "implicit" declaration of a variable y. In actual programming, this error is sometimes difficult to detect.
In addition, today I learned about another issue in this "implicit declaration" through my colleague's introduction.
When you make this "implicit" declaration in the current context, the JavaScript engine will first look for whether this variable has been declared before in the current context. If not, then search in the previous level of context. If it has not been found, it will finally declare this variable on the window!
for example:
window. y = "hello";
function func(){
y = "OH, NO!!!";
}
func();
alert(); //#=> display "OH, NO!!!"
When any layer in the context has such an "implicit" definition of variable, the variable of that layer will be modified without generating a new variable on the window. (This kind of bug is quite annoying, especially the encapsulated more complex code)
for example:
var x = "";
function a() {
var x = "a's x";
var b = function() {
var c = function() {
//no var!
x = "c's x:";
};
alert("before c run,the :" + x);
c();
alert("after c run, the :" + x);
};
alert(" is:" + x);
b();
alert("after b function runed, the is:" + x);
};
alert("before a run, :" + x);
a();
alert("after a run, :" + x);
There are the following layers here: window, func a, func b, func c is always nested at levels. window->a->b->c
Both window and a define variable x, and b is not defined. An x is declared in c, which eventually modifies the value of the variable a.
Remember, in JavaScript, declare variables, and you must add var before it.
y ="xxx";
And so on. There is a problem with this. For example, in a line in the code, I want to use a declared variable x. As a result, due to typing or spelling errors, this variable is written as y. The result is equivalent to an "implicit" declaration of a variable y. In actual programming, this error is sometimes difficult to detect.
In addition, today I learned about another issue in this "implicit declaration" through my colleague's introduction.
When you make this "implicit" declaration in the current context, the JavaScript engine will first look for whether this variable has been declared before in the current context. If not, then search in the previous level of context. If it has not been found, it will finally declare this variable on the window!
for example:
Copy the codeThe code is as follows:
window. y = "hello";
function func(){
y = "OH, NO!!!";
}
func();
alert(); //#=> display "OH, NO!!!"
When any layer in the context has such an "implicit" definition of variable, the variable of that layer will be modified without generating a new variable on the window. (This kind of bug is quite annoying, especially the encapsulated more complex code)
for example:
Copy the codeThe code is as follows:
var x = "";
function a() {
var x = "a's x";
var b = function() {
var c = function() {
//no var!
x = "c's x:";
};
alert("before c run,the :" + x);
c();
alert("after c run, the :" + x);
};
alert(" is:" + x);
b();
alert("after b function runed, the is:" + x);
};
alert("before a run, :" + x);
a();
alert("after a run, :" + x);
There are the following layers here: window, func a, func b, func c is always nested at levels. window->a->b->c
Both window and a define variable x, and b is not defined. An x is declared in c, which eventually modifies the value of the variable a.
Remember, in JavaScript, declare variables, and you must add var before it.