undefined In JavaScript, if we want to determine whether a value is undefined, we usually write: if(a === undefined){ //code} However, undefined in JavaScript is not reliable. Let's try to write a function like this: function test(a) { var undefined = 1; console.log(undefined); // => 1 if(a === undefined) { // ... } } As you can see, undefined is easily changed to 1, which makes our subsequent understanding of undefined ambiguous. Therefore, in JavaScript, it is risky to interpret undefined directly as "undefined" because the identifier may be tampered with. Before ES5, the global undefined can also be modified. In ES5, this identifier is designed to be a read-only identifier. If your current browser is not too old, you can enter the following statement in the console to test it: undefined = 1; console.log(undefined); // => undefined Now we can clearly see that the identifier undefined does not really reflect "undefined", so we have to obtain this semantics through other means. Fortunately, JavaScript also provides the void operator, which evaluates the specified expression and returns the trusted undefined: void expression The most common usage is to obtain undefined through the following operation, and the operation cost is the smallest when the expression is 0: void 0; // or void(0); All places where undefined is needed are replaced by void 0 Of course, there is another way to get undefined: those who have read the jQuery source code should know the immediately executed function in it: (function(window,undefined) { // ... })(window) In this function, we did not pass it the second parameter (the parameter name is undefined), so the value of the second parameter will be considered "undefined". Therefore, in this way, all undefined in the scope of this function are trusted undefined. The above is the details of JavaScript's unreliable undefined. For more information about JavaScript's unreliable undefined, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: An example of installing MySQL on Linux and configuring external network access
>>: 5 Easy Ways to Free Up Space on Ubuntu
1. Dynamic Components <!DOCTYPE html> <h...
Strictly speaking, nginx does not have a health c...
Table of contents 1. v-text (v-instruction name =...
1. Introduction to yum Yum (full name Yellow dogU...
1. What is the hyperlink icon specification ?<...
Table of contents Linux environment variables and...
Table of contents Environment Preparation Environ...
Table of contents Getting Started with MySQL MySQ...
<br />There is no road in the world. When mo...
I have written an article about mobile adaptation...
Record some of the processes of using node-media-...
This article shares the specific code for the WeC...
Table of contents Preface Enumerable properties I...
Database SQL optimization is a common problem. Wh...
Table of contents 1. Object literals 2. The new k...