no-var

需要 letconst 而不是 var

一些该规则报告的问题可以通过 --fix 命令行选项 自动修复

ECMAScript 6 允许程序员使用 letconst 关键字创建具有块范围而不是函数范围的变量。块作用域在许多其他编程语言中很常见,可以帮助程序员避免错误,例如:

var count = people.length;
var enoughFood = count > sandwiches.length;

if (enoughFood) {
    var count = sandwiches.length; // accidentally overriding the count variable
    console.log("We have " + count + " sandwiches for everyone. Plenty for all!");
}

// our count variable is no longer accurate
console.log("We have " + count + " people and " + sandwiches.length + " sandwiches!");

规则详情

该规则旨在阻止使用 var,并鼓励使用 constlet

示例

此规则的错误代码示例:

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

var x = "y";
var CONFIG = {};

3vRURIMaWDsQZ7e/enlM+BoZcbwBhexilWg1PCqWbwjDgMyE7sdL9Ewpkadmo3Xh

/*eslint no-var: "error"*/
/*eslint-env es6*/

let x = "y";
const CONFIG = {};

何时不使用

NTV10mD4IsV73iF7KO9rK71QLEyEbMktrmWQ07uiSqGFA8qOj90QhjI6X9r2Mb7MflOvcVnSE4B9BJLbeLy0kR8AjLMWoNIbGDM0b2iYnn6QOBaXYgrp8YiUwPKMWklgrlr/ISZQ5nzRNC4RmyUTF66qtYydBWJRqUJahQcpdYS0WihFhG1gGTKnd4KYfuzwOlK2lEEmVkIzgfOnasXXBRv4096q8P48LN19mpHbwiCcWhfQywUY1pDmoln+bAv0f3Ra39Wdbq6FlBnl34/lvA==