zoukankan      html  css  js  c++  java
  • void 0 含义

    编程实务

       let a ={
                    b:"sufeng"
       }
                
       console.log(a?.b)
       // 等同于
       // a == null ? undefined : a.b
                
       console.log(a?.["b"])

     编译压缩后,代码被替换为

     console.log(a === null || a === void 0 ? void 0 : a.b);
      // 等同于
      // a == null ? undefined : a.b
    
     console.log(a === null || a === void 0 ? void 0 : a["b"]);

    所以讨论下,什么叫 void 0 ?

    void 0 返回undefined, 我们都知道的,但是为什么不直接arguments[0]!==undefined? 查找资料后总结如下

    1、undefined 可以被重写

    undefined 在ES5中已经是全局对象的一个只读(read-only)属性了,它不能被重写。但是在局部作用域中,还是可以被重写。

    let aaa;
    let undefined = "sufeng";
    
    console.log("undefined ...",undefined);
    console.log("aaa ...", aaa);
    
    if(aaa==undefined){
        console.log("aaa == undefined" );
    }
    
    if(aaa===undefined){
        console.log("aaa === undefined");
    }
    
    //  undefined ... sufeng
    // aaa ... undefined

    2、那为什么选择void  0 

    void 运算符能对给定的表达式进行求值,然后返回undefined。

    let x;
    if((x === void 0)&&(x === void 1)){
        console.log("x 已声明",x)
    }

    void是不能被重写的,但为什么是void 0 呢,void 0 是表达式中最短的。在有些JavaScript压缩工具在压缩过程中,正式将undefined用 void 0 代替调了。

    未完,待续......
  • 相关阅读:
    linux 下载openssl 亲测有效
    在linux文件中 怎么查看文件的行数
    linux 安装mysql 数据库
    bccomp
    javascript之date
    javascript之函数
    JavaScript数据类型之数组
    BOM
    canvas
    javascript数据类型之对象
  • 原文地址:https://www.cnblogs.com/zhishiyv/p/14705722.html
Copyright © 2011-2022 走看看