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 代替调了。

    未完,待续......
  • 相关阅读:
    Windows 8 C#调用C++编写的Windows运行时组件
    Metro style App Datetime formating.
    《编程匠艺》读书笔记之一
    ContextMenu的使用
    单例模式
    c# 类中字段属性设计
    Metro style app 文件查找
    Win 8 学习资料汇总
    Metro C++ 初体验 第二周
    Metro style app 文件、文件夹的选择、文件的保存。
  • 原文地址:https://www.cnblogs.com/zhishiyv/p/14705722.html
Copyright © 2011-2022 走看看