const vs let when calling require

后端 未结 4 2074
盖世英雄少女心
盖世英雄少女心 2021-02-01 01:08

As io.js now supports ES6 you are finally able to use the const and let keywords. Obviously, let is the successor of var, jus

4条回答
  •  灰色年华
    2021-02-01 02:01

    I have the same feeling that you're describing. A big percentage of declared variables in my code tend to be constant, even objects and arrays. You can declare constant objects and arrays and still be able to modify them:

    const arr = [];
    arr.push(1);
    arr;
    // [ 1 ]
    
    const obj = {};
    obj.a = 1;
    obj;
    // { a: 1 }
    

    AFAIK ES6 modules do not require variable declarations when importing, and I suspect that io.js will move to ES6 modules in a near future.

    I think this is a personal choice. I'd always use const when requiring modules and for module local variables (foo in your example). For the rest of variables, use const appropriately, but never go mad and use const everywhere. I don't know the performance between let and const so I cannot tell if it's better to use const whenever possible.

提交回复
热议问题