对象或数组的首选声明约定是什么:const 还是 let?

What is the preferred declaration convention for objects or arrays: const or let?

我不是在问什么是技术上可行的;我知道你能做到

const a = [];
const b = {};
a.push['sup'];
b.test = 'earth';

我想知道的是,当涉及到将修改其内部结构的数组和对象时,是否有任何约定优先使用 let 而不是 const。如果您看到一个用 const 声明的对象,您是否认为该对象的意图是不可变的,并且您更愿意看到 let,或者,因为一些 linters(如 tslint)有一个问题,是否最好只用 const 声明它并相信阅读代码的任何其他人都知道这并不意味着它是不可变的?

这可能是一个您无法获得最终答案的问题,因为它非常基于意见。

一点历史背景有助于理解令人困惑的 const 是如何出现的:

Allan Wirfs-Brock, the former editor of the EcmaScript spec, himself said:

In a slightly different (and perhaps better) universe, ECMAScript const declaration would be named let and introduce a SSA binding.

然后问,为什么不是这样,he responded:

let/const was a carry over from ES4 proposals and Moz JS1.7

TC39 never seriously explored naming alternatives

所以你可以说 const 有点用词不当,因为它没有做不熟悉它的人可能期望它做的事情。

由于 const 按照它的方式工作并且几乎肯定不会改变,我仍然会用 const 定义你的对象。

这可能有助于不熟悉 const 工作方式的人更好地理解。

也许在对象第一次出现 const 时添加注释可能会有所帮助,大致如下:

// Important: 'const' only ensures that 'styles' always references the same object,
// it does not render it immutable
const styles = {};

虽然意见可能会有所不同。

I favor const over let in ES6. In JavaScript, const means that the identifier can’t be reassigned. (Not to be confused with immutable values. Unlike true immutable datatypes such as those produced by Immutable.js and Mori, a const object can have properties mutated.)

If I don’t need to reassign, const is my default choice over let because I want the usage to be as clear as possible in the code.

I use let when I need to reassign a variable. Because I use one variable to represent one thing, the use case for let tends to be for loops or mathematical algorithms.

参考:https://medium.com/javascript-scene/javascript-es6-var-let-or-const-ba58b8dcde75#.9s2e58ihw

来自 MDN 的示例: const 声明创建对值的只读引用。并不是说它持有的值是不可变的,只是变量标识符不能被重新赋值。

const name1 = value1 [, name2 = value2 [, ... [, nameN = valueN]]];

nameN The constant's name, which can be any legal identifier. valueN The constant's value; this can be any legal expression.

let 语句声明一个块作用域局部变量,可选择将其初始化为一个值。

let var1 [= value1] [, var2 [= value2]] [, ..., varN [= valueN]];

var1, var2, …, varN Variable name. It can be any legal identifier. value1, value2, …, valueN Initial value of the variable. It can be any legal expression.

在我看来,这完全取决于您想用它们做什么。我发现上面的陈述足够有说服力,可以与您分享。

对象前面的 const 关键字表示有一个对象,并且您正在使用 references 来改变它。它还(正确地)暗示您不应尝试重新分配对此对象的引用。

const obj = {a: 'foo', b: 'bar'};

const obj2 = {z: 'baz'};

obj = obj2; // const will prevent this operation. 

const 并不意味着不应更改对象属性。这确实意味着您不应尝试更改引用。

如果您打算重新分配对对象的引用,则使用 let

来源:AirBnB Javascript Style Guide

没有首选,这取决于您对该数组或对象的用法选择。你必须清楚地理解突变和重新分配。

Mutation - updates the values present in the memory

Reassign - variable points to new memory locations where new values are stored

让 - 提供突变和重新分配

Const - 提供变异但不重新分配

两者 - 不提供重新声明

如果你的用例只需要改变,你可以去const..如果你需要重新分配然后去让。

// LET

let condiments = ['Ketchup', 'Soy Sauce', 'Sriracha'];

// Mutation possible
condiments[0] = 'Mayo';
console.log(condiments);//=> [ 'Mayo', 'Soy Sauce', 'Sriracha' ]

// Re-assigning possible
condiments = ['Mayo'];
console.log(condiments); //=> [ 'Mayo' ]

// Re-declaring not possible
//let condiments = [] //=> SyntaxError: Identifier 'condiments' has already been declared


// CONST

const utensils = ['Fork', 'Chopsticks', 'Spork'];

// Mutation Possible
utensils[2] = 'Spoon'
console.log(utensils); //=> [ 'Fork', 'Chopsticks', 'Spoon' ]
utensils.length = 0
console.log(utensils); //=> [ ]

// Re-assigning not possible
//utensils = ['Spoon']; //=> TypeError: Assignment to constant variable.

// Re-declaring not possible
//const utensils = {} //=> SyntaxError: Identifier 'condiments' has already been declared