Written in front of the still ambiguous statement: JavaScript in this article refers to the general meaning of JavaScript, and not only the "self-proclaimed JavaScript" of the runtime environment; Global variables and global object properties refer to the same thing, Just because we have to cooperate with the context, we use different theories. In the text I will no longer explain the other; "declaration" means declaring a variable and/or a definition of a function and its signature through the "var" statement; "variable" refers to a named parameter declared through a "var" statement or attempted to be accessed in a function body; "Undefined" The value named "Undefined" (global or local), and "undefined" refers to the concept value of type (...) = = "undefined"; "Output" is a function that shows us incoming arguments, in fact, the same kind as "alert".
The first is a method that is similar to void (0), and we know that attempting to access any function that has no return value in JavaScript will get the concept value "undefined", so we can do this:
MyVar = function () {} ();
The principle of this method is to create an empty function and get its (non-existent) return value. Obviously, we get the concept value "undefined", which is the same as void (0).
It is important to note that this invocation can only be used if the function keyword is not at the top of the statement, and that you should do so if you need to call an anonymous function directly:
(function () {
code here
})();
If we don't want to pollute the global scope, we can create a closure in this way--many of the existing JavaScript libraries do that.
This approach was born without understanding the effect of void (0), and since we know Void (0), this is not necessary.
The second, less common approach is to access window.undefined, just like the bottom:
Output (MyVar = = window.undefined);
MyVar = window.undefined;
The rationale for this approach is:
Window.undefined can be accessed directly if JavaScript's operating environment has predefined undefined values;
Without a predefined value for undefined, window.undefined returns the concept value "undefined"-or what we want.
After the Access form has changed from "global" to "attribute", the script engine does not consider this an unexpected operation, so the code can execute correctly.
However, this approach is still inadequate:
Undefined is not a reserved word in all JavaScript engines, which means it can be contaminated, which affects the performance of our program;
The direct access to the properties of the global object is inefficient and should be avoided.
So here's the most interesting way I've ever seen it--declaring undefined variables locally!
Its approach is this:
function MyFunc () {
var undefined;
Some code here
Output (MyVar = = undefined);
MyVar = undefined;
}
Speaking of which, I would like to mention that some people have a different understanding of the phrase "there is no undefined in the early browsers" and that this is not feasible.
And I think this is wrong, in my impression (about 2003), IE5 although not predefined undefined, but does not affect our unassigned statements.
But after all it was many years ago, the memory blurred, in order to avoid the mistake caused by the assumption, I specially installed the Windows 98来 experiment:
The above two is using the QQ screen interception, do not know why became a jpg, the bottom of this is a piece of OneNote interception.
It can be inferred from this that it is superfluous to worry about not assigning declarations undefined variables.
As explained in JavaScript, if you try to read a variable that is not predefined or not declared, a "nonexistent (undefined)" exception is thrown, so it is possible to fail to access undefined directly on an earlier browser.
This phenomenon (which throws an exception) may be intended to be designed for ease of troubleshooting, and for a declared variable, even if it is not assigned, an exception is not thrown at the time it is read-a declaration that the developer has identified "this thing."
This is the practice of declaring local variables undefined within a function scope, not only does it solve the problem that references undeclared variables can cause an exception, but also avoids the effects of global variable pollution, and replaces void (0), which is not only a hidden trick that does not create an imperceptible spelling error, can also improve the efficiency of the operation-is really a good coup n!
So know so much later found that the last introduction of this approach is home travel, killing the first choice
If someone asks, "How should undefined be used?" "We can answer now: just use it!"
At last, some people may be tempted to ask: Why does my code example in this series compare "strict equality" with three equal signs? What is the problem with the ordinary "equality" of two equals?
This is what the next chapter of this series is going to explain: undefined and its various equivalent forms of applications.