no-void

禁止 void 运算符

void 运算符接受一个操作数并返回 undefinedvoid expression 将评估 expression 并返回 undefined。它可以用来忽略 expression 可能产生的任何副作用:

使用 void 运算符的常见情况是获取 "pure" undefined 值,因为在 ES5 之前,undefined 变量是可变的:

// will always return undefined
(function(){
    return void 0;
})();

// will return 1 in ES3 and undefined in ES5+
(function(){
    undefined = 1;
    return undefined;
})();

// will throw TypeError in ES5+
(function(){
    'use strict';
    undefined = 1;
})();

另一种常见的情况是缩小代码,因为 void 0undefined 短:

foo = void 0;
foo = undefined;

当与 IIFE(立即调用的函数表达式)一起使用时,void 可用于强制将 function 关键字视为表达式而不是声明:

var foo = 1;
void function(){ foo = 1; }() // will assign foo a value of 1
+function(){ foo = 1; }() // same as above
function(){ foo = 1; }() // will throw SyntaxError

qhr/jywpw6s7vuB/YCmi2mbvnZ1DEf4q0OxqYMgJhkh2fqwqK4kbm/f7SN94R3zecQVLUWv8DN4Bd3yRMAwL/F7E7bsuVw0aRkJs5CIZoO8/cVkxa5l80HtGshOkkNp1qmvbFdiqTtUGu6j6BIyY+g==

规则详情

Zt2CZvKri7cDQOPYcO0mlJP/dWVrNTQPO1s2pQt9XJAulhgWoTvbWEQu1reBMwC7

uKrz8SURVDVUumtIHaN5gqxWgNYdvTrkXYbJoWMZU4Gpko6+oAyaLHgE+hkdxTLB

/*eslint no-void: "error"*/

void foo
void someFunction();

var foo = void bar();
function baz() {
    return void 0;
}

选项

i/QWu02im19/lo5J+rk5/ZR4XOv+gnAKNAo2YIGQQRhKcl4h+hJrX6M9JueM06zl

    VCGPT5g1B8LKsZX6bFgwHV5KRT7Y9FYEKb1dVKxsWCu25tz2Kn4WFcMircGE2VqPNATvYYKsejPj/MxcvEX2O7Gtse6LxEYaDeEccXqY7g42EJJInA568loiT1ghyfidKvPYJnOJetvsW08sEL5VqaBayI6UPO+6RjREtYoUBzjO4TrsZisyq6LuS/bBtaCn

allowAsStatement

BrTM/uI6L1m2g4kb4WQ5YmURunH7qr59bRG9IwkpEKiauGy5/OhRqmSMJOn9dHGeMP2J9wm8xiZGokBTu0o29YhTbzbHuhRVq1DuvZCiXiVf5ZsaFXbMwrfnRq4lQ1gJgTnETuPv3FH/o+bcW+5mQ//Go+N78rMAJmKmi9e9frt5VH0mEF5AVOHUIawyadhsMLUXvUhzpLPfqqQD5xWorRfvyVbqFFKV+iefn7itaDITX5dzSWMKgw+JIo3JK18hlWSV+q3fkl5xUbUbPZlKJg==

t2GuO4w9reC55vxUtLW+5i6kSP0MPbmIhhOIXHH7wDGk50tVZ1frqSQ8NBbcXGKOLRmmcbsgThHn6v72KUPI+APHq34ksvhLHnorENYr8hM=

/*eslint no-void: ["error", { "allowAsStatement": true }]*/

var foo = void bar();
function baz() {
    return void 0;
}

t2GuO4w9reC55vxUtLW+5i6kSP0MPbmIhhOIXHH7wDE5DoU9ggO1XicHJpZPxKmBONaDVKtFE36qFCNmdz9E5fHkNFK2rcGdo8Xy33F6bCE=

/*eslint no-void: ["error", { "allowAsStatement": true }]*/

void foo;
void someFunction();

何时不使用

DPP4c4eaQXPsAA0fXMOoTHoi1DKodAF7KnIP9MyvgWXz50qQK9Bg/p4kb/BwFCPa9KpqhPWY7i+yhCg0K+Eh/tE/UnC1pvsoXTQGqtPJmMU=