[JavaScript standard built-in objects] - value attribute

Briefly

These built-in global properties return a simple value that has no properties or methods of its own.

The value properties of the JavaScript standard built-in objects are:

  • globalThis
  • Infinity
  • NaN
  • undefined

globalThis

The global attribute globalThis contains the global this value, similar to the global object (global object).
globalThis provides a standard way to get the global this object (that is, the global object itself) in different environments. Unlike properties such as window or self, it ensures that it works with and without a window. Therefore, you can use globalThis without worrying about its operating environment. For mnemonics, you just need to remember that this in the global scope is globalThis.
insert image description here

In the node environment, globalThis has the same value as the global object.

insert image description here

In the web environment, globeThis has the same value as the window object.

insert image description here

Infinity

The global property Infinity is a numeric value representing infinity.
The initial value of Infinity is Number.POSITIVE_INFINITY (constant, representing infinity). Infinity (positive infinity) is greater than any value.
insert image description here

NaN

The global property NaN is a value representing not-a-number.
The initial value of NaN is not a number—the same value as Number.NaN. In modern browsers, NaN is a non-configurable, non-writable property. Even if it's not, avoid overriding it. NaN is rarely used in programs.
There are five different types of operations that return NaN :

  • Failed number conversion (for example, an explicit conversion like parseInt("blabla"), Number(undefined), or an implicit conversion like Math.abs(undefined))
  • Mathematical operations that do not evaluate to real numbers (for example, Math.sqrt(-1))
  • Infinity (for example, 0 * Infinity, 1 ** Infinity, Infinity / Infinity, Infinity - Infinity)
  • A method or expression whose operands are coerced to NaN (for example, 7 ** NaN, 7 * "blabla") - this means NaN is contagious
  • Other cases where invalid values ​​are represented as numbers (e.g. invalid Date new Date("blabla").getTime(), "".charCodeAt(1))

The characteristics of NaN are as follows :

  • If NaN involves mathematical operations (but not bitwise operations), the result is usually NaN as well
  • When NaN is one of the operands of any relational comparison (>, <, >=, <=), the result is always false
  • NaN is not equal to (via、!=、= and !==) any other value - including with another NaN value

NaN is also one of the false values ​​in JavaScript.
insert image description here
Difference between isNaN() and Number.isNaN() : The former returns true if the current value is, or would be if it were cast to a number, NaN. And the latter is only true if the value is currently NaN:
insert image description here
Also, some array methods cannot find NaNs, while others can. That is, looking up the index (indexOf(), lastIndexOf()) cannot find NaN, while looking up the value (includes()) can:

/**
 * 此外,一些数组方法不能找到 NaN,而另一些可以。也就是说,查找索引的(indexOf()、lastIndexOf())不能找到 NaN,而查找值的(includes())可以:
 */
const arr = [2, 4, NaN, 12];
arr.indexOf(NaN); // -1
arr.includes(NaN); // true

// Methods accepting a properly defined predicate can always find NaN
arr.findIndex((n) => Number.isNaN(n)); // 2

NaNs are propagated through mathematical operations, so usually testing for NaNs once at the end of a computation is sufficient to detect error conditions. The only time NaN is silently escaped is when exponentiating with an exponent of 0, which immediately returns 1 without testing the value of the base.

NaN ** 0 === 1; // true

undefined

undefined is a property of the global object. That is, it is a variable at the global scope. The initial value of undefined is the primitive data type undefined.
The type of a variable that has not been assigned a value is undefined, and the value is also undefined (indicating that the variable is only declared and not assigned a value). If the method or the variable operated in the statement has not been assigned, undefined will be returned.

function test(a){
    
    
    console.log(typeof a);    // undefined
    return a;
}

test();                       // 返回"undefined"

If a function does not use the return statement to specify the return value, it will return an undefined value.
It is a very bad idea to use it as an identifier (variable name) in a non-global scope (because undefined is not a reserved word (en-US))), because it will make your code difficult to read Maintenance and troubleshooting.

// 不要这样做!

// 打印 'foo string' PS:说明 undefined 的值和类型都已经改变
(function() {
    
    
var undefined = 'foo';
console.log(undefined, typeof undefined)
})()

// 打印 'foo string' PS:说明 undefined 的值和类型都已经改变
(function(undefined) {
    
    
console.log(undefined, typeof undefined)
})('foo')

You can use undefined and the strict equality or inequality operators to determine whether a variable has a value. In the code below, the variable x is undefined and the if statement will evaluate to true.

var x;

if (x === undefined) {
    
    
// 执行这些语句
} else {
    
    
// 这些语句不会被执行
}

Alternatively, typeof can be used:

var x;
if(typeof x === 'undefined') {
    
    
    // 执行这些语句
}
// 使用 typeof的原因是它不会在一个变量没有被声明的时候抛出一个错误。
// 这里没有声明 y
if(typeof y === 'undefined') {
    
           // 没有错误,执行结果为 true
   console.log("y is " + typeof y )  // y is undefined
}

if(y === undefined) {
    
                    // ReferenceError: y is not defined

}

JavaScript is a statically scoped language, so whether a variable is declared can be determined by whether it is declared in an enclosing context. The only exception is the global scope, but the global scope is bound to the global object, so to check whether a variable exists in the global context, you can check whether this property exists on the global object (such as using the in operator) .

if ('x' in window) {
    
    
  // 只有 x 被全局性的定义 才会执行这些语句
}

The void operator is a third alternative. The void operator returns undefined.

var x;
if(x === void 0) {
    
    
    // 执行这些语句
}

// 没有声明 y
if(y === void 0) {
    
    
    // 抛出一个 RenferenceError 错误 (与`typeof`相比)
}

epilogue

it's over.

Guess you like

Origin blog.csdn.net/qq_43231248/article/details/131550657