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
Copy code The code is as follows: <!DOCTYPE ht...
This article example shares the specific code for...
<br />Previous article: Seven Principles of ...
Table of contents Dynamically change themes The f...
This article example shares the specific code of ...
Step 1: Confirm the architecture of your system d...
After MySQL is installed, you can verify whether ...
meta is an auxiliary tag in the head area of htm...
1. Add the following code to http{} in nginx.conf...
MJML is a modern email tool that enables develope...
This article does not have any quibbles, it is jus...
This article shares the specific code of JavaScri...
Effect: The title has its own serial number, the ...
When the scroll bar is pulled down, the floating ...
There is no data directory, my-default.ini and my...