TreeviewCopyright © aleen42 all right reserved, powered by aleen42

References Back

1. Use const

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 of var.
  • 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 and let are block-scoped(塊作用域).
{
    let a = 1;
    const b = 1;
}
console.log(a); /** ReferenceError  */
console.log(b); /** ReferenceError  */

results matching ""

    No results matching ""