TreeviewCopyright © aleen42 all right reserved, powered by aleen42
References Back
1. Use const
- To use
const
for all of your references, and avoid usingvar
. - Eslint rules tags:
prefer-const
,no-const-assign
the reason is to ensure that you cannot reassign(重新訪問) references, which can lead to bugs and difficult to comprehend(理解) code.
/**
* bad
*/
var a = 1;
var b = 2;
/**
* good
*/
const a = 1;
const b = 2;
2. Use let
- If you must reassign references, you can use
let
instead ofvar
. - Eslint rules tags:
no-var
/**
* bad
*/
var count = 1;
count = count? count + 1 : count;
/**
* good
*/
let count = 1;
count = count? count + 1 : count;
- Notice that: both
const
andlet
are block-scoped(塊作用域).
{
let a = 1;
const b = 1;
}
console.log(a); /** ReferenceError */
console.log(b); /** ReferenceError */