"Global variables" and "attributes of Global Objects" refer to the same thing, but they are different because they need to be used in combination with the context. I will not explain them in the body; "Declaration" refers to declaring variables and/or defining functions and their signatures through the "var" statement; "variable" refers to the name parameter that has been declared through the "var" statement or tried to access in the function body; "undefined" refers to the value named "undefined" (global or local variable ), "undefined" refers to type (...) = "undefined"; "output" is a function that displays input parameters and can be seen as similar to "alert.
When writing JavaScript programs, we always need to use "undefined". For example, we need to know whether a value has been assigned a value, or if we want to remove a value that has already been assigned, we may do this:
Copy codeThe Code is as follows:
Output (myVar = undefined );
MyVar = undefined;
However, this is not very good. If we try to "read" (or compare) a non-existent variable, an exception will be thrown.
For example, if the variable myVar has not been defined when the above code is executed due to incomplete design, the above Code will go wrong;
In addition, the predefined undefined value does not exist in earlier browser versions. To improve compatibility and fault tolerance, we can do this:
Copy codeThe Code is as follows:
Output (typeof (myVar) = "undefined ");
MyVar = void (0 );
The typeof operator is a JavaScript language function, although the above Code looks like something like this:
Copy codeThe Code is as follows:
Output (oneFunction (myVar) = "undefined ");
MyVar = void (0 );
However, there is an important difference between the two-When myVar does not exist, the former can be correctly executed and returns the string "undefined". The latter will cause an exception regardless of the internal structure.
Void is another language function. The meaning of this operator is to hide its input parameters from other parts of the script. If a statement tries to obtain the void "operation" result, it can only get "undefined ".
Because of this feature of void, the most common functions of void are as follows: one is to assign the "undefined" value to other variables/attributes as in the code above; the other is as follows:
<A href = "javascript: void (favList. push (curItem);"> Add to Favorites list </a>
FavList is a JavaScript array, while curItem is a defined object. The array push method returns the length of the array after the push operation is executed, in this example, the length is useless at all, but if we leave it alone, the browser may jump to a script result page that is almost blank, only the return value of push is displayed, for example, "3 ".
Therefore, we need to use a void operator to "cheat" the browser: nothing here.
Looking back at the use of "undefined", we will find that we used the typeof operator that is "highly fault tolerant" compared with "undefined", which is a little troublesome to use: A total of 10 characters such as "typeof ()" "need to be written.
Although we are programmers dealing with troubles all day long, this does not mean we should not try to reduce the trouble ~ Aren't all development tools designed to make programming simple?
Therefore, if you clearly know that a variable must have been declared, you can directly compare the variable with the known "undefined", for example:
Copy codeThe Code is as follows:
Output (myVar = void (0 ));
Compared with the typeof operator, this method can not only play a few characters, but also has some advantages: it can avoid imperceptible spelling errors (for example, writing "undefined" into "undefinied" or something ).
The most common tips on "undefined"/"undefined" have been described.
In the next article, I will introduce some less common skills.