Home >Web Front-end >JS Tutorial >Write better JavaScript - undefined (Part 1)_javascript skills

Write better JavaScript - undefined (Part 1)_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:41:331075browse

"Global variables" and "properties of global objects" refer to the same thing, but different terms are used to match the context. I will not explain it in the text; "declaration" refers to declaring variables through the "var" statement. and/or a definition of the function and its signature; "variable" refers to a named parameter declared via a "var" statement or attempted to be accessed in the function body; "undefined" refers to a value (global or local variable) named "undefined", "Undefined" refers to the situation where type(...) == "undefined"; "output" is a function that displays the incoming parameters and can be regarded as the same kind as "alert".

When we write JavaScript programs, we always need to use "undefined". For example, we need to know whether a certain value has been assigned, or we want to eliminate a certain value that has been assigned. We It is possible to do this:

Copy the code The code is as follows:

output(myVar === undefined );
myVar = undefined;

However, this is not very good. If we try to "read" (or compare) a variable that does not exist, an exception will be thrown.
For example, if the variable myVar has not been defined when executing the above code due to insufficient design, the above code will go wrong;
In addition, in earlier browser versions, there is no undefined This predefined value, so to improve compatibility and fault tolerance, we can do this:
Copy code The code is as follows:

output(typeof(myVar) == "undefined");
myVar = void(0);

The typeof operator is a language feature of JavaScript, although the above The code looks something like this:
Copy the code The code is as follows:

output(oneFunction(myVar) == "undefined");
myVar = void(0);

But there is an important difference between the two - when myVar does not exist, the former It can be executed correctly and returns the string "undefined"; the latter will throw an exception regardless of the internal structure.
void is another language feature. The meaning of this operator is to hide its incoming parameters from other parts of the script; and if there is a statement trying to get the result of void "operation", it can only get "unknown" definition".
Because of this characteristic of void, there are two most common functions of void: one is to assign "undefined" values ​​to other variables/properties like in the code above; the other is like the following:
Add to favorites list
Where favList is a JavaScript array, and curItem is an already defined object— —The push method of the array will return the length of the array after the push operation. In this example, this length is of no use to us, but if we leave it alone, the browser may jump to an almost blank The script result page only displays the return value of push, such as "3".
So you need to use a void operator to "cheat" the browser: there is nothing here.
Now returning to the use of "undefined", you will find that the "high fault tolerance" typeof operator we used to compare with "undefined" is a bit troublesome to use: it requires more writing in total "typeof()""" is 10 characters.
Although we are programmers who deal with trouble all day long, this does not mean that we should not find ways to reduce trouble~ Aren’t various development tools actually created to make programming work easier?
So if you know clearly that a variable must have been declared, you can directly compare the variable with the known "undefined", for example:
Copy code The code is as follows:

output(myVar === void(0));

Compare Compared with using the typeof operator, this latter method not only saves typing a few characters, but also has some advantages: it can avoid difficult-to-detect spelling errors (for example, writing "undefined" instead of "undefined" or something).

At this point, the most common techniques regarding "undefined"/"undefined" have been introduced.
In the next article, I will introduce some other less common techniques.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn