void 0 与 undefined
今天在看 blueBird的wiki 时,看到了 void 0,百度之后才发现它不简单。
undefined
在 ES5 中undefined是一个全局对象的只读(read-only)属性,但是在局部作用域中,还是可以被重写的,例如:
(function () {
var undefined = 1
console.log(undefined);
})()
void 0
The void operator evaluates the given expression and then returns undefined.
void 运算符能对给定的表达式进行求值,然后返回 undefined。也就是可以对任意表达式求值都会获得 undefined,void 123、void 'hello' 都可以,而 void 0 是最短的最省空间,所以很多压缩工具都会将 undefined 转化为 void 0