在js中定义一个const数组是什么意思?

Ary*_*ary 2 javascript arrays reactjs

当我们在 javascript 中将数组定义为常量时,是否意味着数组不能缩小或放大并且具有恒定大小,或者,是否意味着数组中的所有元素都是常量并且您不能更改它们的值。

handleClick(i) {
     const squares = this.state.squares.slice();
     squares[i] = 'X';
     this.setState({squares: squares});
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中。

Rob*_*sen 5

将变量声明为constonly 意味着一旦分配了值,您就不能为该变量分配新值:

const array = [];

array = []; // Not allowed: assignment to constant variable
Run Code Online (Sandbox Code Playgroud)

将数组声明const为与实际数组的内容无关:

const array = [];

array.push("something"); // Allowed: add value to array
array[0] = "or other";   // Allowed: replace value in array
array.length = 0;        // Allowed: change array size
Run Code Online (Sandbox Code Playgroud)