引用

2.1所有的引用值都是使用 const,避免使用var

eslint:prefer-constno-const-assign

为什么?因为这样可以保证你的引用值不会被修改覆盖,覆盖引用值会引发bug,也会使代码的可读性变差。

//bad
var a=1;
var b=2;

//good
const a=1;
const b=2;

2.2如果你确实需要重写引用值,使用 let 代替 var

eslint: no-varjscs:disallowVar

为什么?因为 let 是块作用域,这比var的函数作用域要好。

// bad
var count=1;
if(true){
    count+=1;
}

//good, use the let
let count=1;
if(true){
    count+=1
}

2.3 注意,letconst都是块级作用域

// const and let only exist in the blocks they are defined in
{
    let a=1;
    const b=1;
}
console.log(a); //ReferenceError
console.log(b); //ReferenceError

results matching ""

    No results matching ""