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

Blog    

Recommend

Learning Vue instructions

Table of contents 1. v-text (v-instruction name =...

How to use the yum command

1. Introduction to yum Yum (full name Yellow dogU...

Hyperlink icon specifications: improve article readability

1. What is the hyperlink icon specification ?<...

Introduction to Linux environment variables and process address space

Table of contents Linux environment variables and...

CentOS 7.6 installation of MySQL 5.7 GA version tutorial diagram

Table of contents Environment Preparation Environ...

MySQL Basics in 1 Hour

Table of contents Getting Started with MySQL MySQ...

Web designer is a suitable talent

<br />There is no road in the world. When mo...

Use Rem layout to achieve adaptive

I have written an article about mobile adaptation...

Use node-media-server to build a simple streaming media server

Record some of the processes of using node-media-...

WeChat applet implements the Record function

This article shares the specific code for the WeC...

Detailed discussion of the differences between loops in JavaScript

Table of contents Preface Enumerable properties I...

3 ways to create JavaScript objects

Table of contents 1. Object literals 2. The new k...