转自:https://blog.csdn.net/juzipchy/article/details/86367565

undefined 在局部作用域中可以被重写

(function() {
  var undefined = 10;
 
  // 10 -- chrome
  alert(undefined);
})();

所以直接赋值不再安全

 

而对于void:

The void operator evaluates the given expression and then returns undefined.

1、void 运算符能对给定的表达式进行求值,然后返回 undefined。也就是说,void 后面你随便跟上一个表达式,返回的都是 undefined,如 void (2), void (‘hello’)。

2、void是不能被重写的。但为什么是void 0 呢,void 0 是表达式中最短的。

其实不少 JavaScript 压缩工具在压缩过程中,正是将 undefined 用 void 0 代替掉了。

 

相关文章:

  • 2022-02-28
  • 2022-12-23
  • 2021-12-12
  • 2021-05-17
  • 2022-12-23
  • 2021-09-05
  • 2022-12-23
  • 2021-08-31
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-13
  • 2021-10-02
  • 2022-12-23
  • 2021-08-28
相关资源
相似解决方案