JavaScript's unreliable undefined

JavaScript's unreliable undefined

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:
  • 7 tips for dealing with undefined JavaScript values
  • JavaScript undefined and null difference example analysis
  • Analysis of null and undefined usage in JavaScript
  • The essence of undefined in javascript
  • Solution to the problem that the return value of showModalDialog function in JS Chrome browser is undefined
  • A brief analysis of undefined and null in the Javascript type system
  • JavaScript type system - a comprehensive understanding of undefined and null
  • Javascript Basics_Simple comparison of undefined and null values
  • JavaScript must know (Part 2) null and undefined
  • Distinguishing Undefined and null types in JavaScript

<<:  An example of installing MySQL on Linux and configuring external network access

>>:  5 Easy Ways to Free Up Space on Ubuntu

Recommend

Perfect solution for vertical centering of form elements

Copy code The code is as follows: <!DOCTYPE ht...

Vue shuttle box realizes up and down movement

This article example shares the specific code for...

Seven Principles of a Skilled Designer (2): Color Usage

<br />Previous article: Seven Principles of ...

Vue implements multiple ideas for theme switching

Table of contents Dynamically change themes The f...

Vue implements the right slide-out layer animation

This article example shares the specific code of ...

Ubuntu 16.04 64-bit compatible with 32-bit programs in three steps

Step 1: Confirm the architecture of your system d...

How to verify whether MySQL is installed successfully

After MySQL is installed, you can verify whether ...

Introduction to the use of http-equiv attribute in meta tag

meta is an auxiliary tag in the head area of ​​htm...

How to configure nginx to limit the access frequency of the same IP

1. Add the following code to http{} in nginx.conf...

Creating Responsive Emails with Vue.js and MJML

MJML is a modern email tool that enables develope...

JavaScript to achieve lottery effect

This article shares the specific code of JavaScri...

Typora code block color matching and title serial number implementation code

Effect: The title has its own serial number, the ...

Example code for implementing page floating box based on JS

When the scroll bar is pulled down, the floating ...

Detailed steps to configure my.ini for mysql5.7 and above

There is no data directory, my-default.ini and my...