Javascript-标准内置对象-值属性-globalThis-Infinity-Nan-undefined 手写实现globalThis功能

1 globalThis

1.1 globalThis简介

        globalThis 是 ECMAScript 2020(ES11)引入的全局对象的标准化引用。在不同JavaScript 运行环境中,全局对象的名称可能不同: 浏览器中是 window。 Node.js 中是 global。 Web Workers 中是 self。 使用 globalThis,你可以在任何环境中统一访问全局对象。

// globalThis
console.log(globalThis)
// 挂载全局对象
globalThis.a = 1
console.log(a)

// 使用全局对象Math Math也是任意环境下都可以使用的
const num = globalThis.Math.random()
console.log(num) 

1.2 手写实现globalThis

        通过条件判断实现一个跨环境的全局对象获取函数,代码如下所示,利用了所有环境都有Math对象挂载到全局对象来判断实现:

function check(val){
    return val && val.Math === Math && val
}

function getGlobal(){
    return check(typeof window === "object" && window) ||
    check(typeof self === "object" && self) ||
    check(typeof global === "object" && global) ||
    (function(){
        return this
    })() || Function("return this")()
}

const myglobal = getGlobal()
console.log(myglobal)

2 Infinity

        Infinity 表示正无穷大,是一个数值类型的全局属性。在数学计算中,当结果超出 JavaScript 能表示的最大数字时,会返回 Infinity。负无穷大用 -Infinity 表示。 Infinity 大于任何数值。与任何数进行算术运算,遵循特定规则。

  • 对Infinity加减都是Infinity,Infinity任意数都是+-Infinity,但是乘法中乘以0的时候是Nan
  • Math.pow(10, 10000)是Infinity因为大于某个值就会成为Infinity,Math.log(0)是-Infinity
  • 常数除以Infinity是0, 1 / 0是Infinity
  • 1**infinity, Infinity * infinity, infinity - infinity也是Nan

        代码示例如下所示:

// Infinity
console.log(Infinity === Number.POSITIVE_INFINITY) // true
console.log(Infinity * 0) // NaN
console.log(Infinity); /* Infinity */
console.log(Infinity + 1); /* Infinity */
console.log(Math.pow(10, 1000)); /* Infinity */
console.log(Math.log(0)); /* -Infinity */
console.log(1 / Infinity); /* 0 */
console.log(1 / 0); /* Infinity */

3 Nan

        NaN 代表 "Not-a-Number"(非数字),是一个特殊的数值类型。当数学运算无法得到有效数值结果时,会返回 NaN。

  • NaN 不等于任何值,包括它自身。
  • 可以使用 isNaN() 函数或 Number.isNaN() 方法来检测是否为 NaN。
  • 如果 NaN 涉及数学运算(但不涉及位运算),结果通常也是 NaN。
  • 当 NaN 是任何关系比较(>、<、>=、<=)的操作数之一时,结果总是 false。
  • NaN 不等于(通过 ==、!=、=== 和 !==)任何其他值——包括与另一个 NaN 值。

 3.1 争对NaN的测试

        测试关系比较运算符,以及==, ===, !==的测试,测试isNaN和Number.isNaN的使用,发现:

  • isNaN会将参数转换为数字判断,Number.isNaN不会
  • NaN !== NaN判断为真,可以使用 x !== x来判断NaN
// NaN
console.log(NaN === NaN) // false
console.log(NaN > 3) // false
console.log(NaN < 3) // false
console.log(NaN == 3) // false
console.log(NaN === 3) // false
console.log(NaN !== NaN) // true

// isNaN
console.log(isNaN(NaN)) // true
console.log(Number.isNaN(NaN)) // true
// Number.isNaN不会将参数转换为数字(只会判断当前状态是否为NaN)
console.log(Number.isNaN("Hello")) // false
// 全局isNaN会将参数转换为数字(包含未来状态)
console.log(isNaN("Hello")) // true

         如果数组当中存在Nan那么有些方法无法查找到NaN的存在,例如indexOf、lastIndexOf,但是查找值的可以,例如includes。

4 undefined

        undefined 表示未定义的值,是 JavaScript 中的一种基本数据类型。当变量声明但未赋值时,其值为 undefined,有以下要点:

  • 访问对象中不存在的属性,返回 undefined。
  • 没有return的函数返undefined。
  • undefined 与 null 不同,null 表示空值。
  • 必须使用严格等于运算符 === 来检测是否为 undefined,因为undefined和null在==下为true。
  • 如果方法或者是语句中操作的变量没有被赋值,则会返回 undefined
  • undefined可以在非全局作用域中被当作标识符(变量名)来使用(因为 undefined 不是保留字)。
let x;
console.log(x);                     // 输出:undefined
const obj = {};
console.log(obj.prop);              // 输出:undefined
function foo() {}
console.log(foo());                 // 输出:undefined
console.log(undefined === undefined); // 输出:true
console.log(null == undefined) // true


// 这里没有声明 y
if (typeof y === "undefined") {
    // 没有错误,执行结果为 true
    console.log("y is " + typeof y); // y is undefined
  }
  
  if (y === undefined) {
    // ReferenceError: y is not defined
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值