void
void 运算符 对给定的表达式进行求值,然后返回 undefined。
语法
void expression描述
这个运算符能向期望一个表达式的值是undefined的地方插入会产生副作用的表达式。
void 运算符通常只用于获取 undefined的原始值,一般使用void(0)(等同于void 0)。在上述情况中,也可以使用全局变量undefined 来代替(假定其仍是默认值)。
立即调用的函数表达式
在使用立即执行的函数表达式时,可以利用 void 运算符让 JavaScript 引擎把一个function关键字识别成函数表达式而不是函数声明(语句)。
void function iife() {
    var bar = function () {};
    var baz = function () {};
    var foo = function () {
        bar();
        baz();
     };
    var biz = function () {};
    foo();
    biz();
}();JavaScript URIs
<a href="javascript:void(0);">
  Click here to do nothing
</a>
<a href="javascript:void(document.body.style.backgroundColor='green');">
  Click here for green background
</a>注意,虽然这么做是可行的,但利用javascript:伪协议来执行 JavaScript 代码是不推荐的,推荐的做法是为链接元素绑定事件。
规范
| Specification | Status | Comment | 
|---|---|---|
| ECMAScript Latest Draft (ECMA-262)The definition of 'The void Operator' in that specification. | Living Standard |  | 
| ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'The void Operator' in that specification. | Standard |  | 
| ECMAScript 5.1 (ECMA-262)The definition of 'The void Operator' in that specification. | Standard |  | 
| ECMAScript 3rd Edition (ECMA-262)The definition of 'The void Operator' in that specification. | Standard |  | 
| ECMAScript 1st Edition (ECMA-262)The definition of 'The void Operator' in that specification. | Standard | Initial definition. Implemented in JavaScript 1.1 | 
浏览器兼容
| Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari | 
|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | 
| Feature | Android | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | 
|---|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | 
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

