javascript定数でこの動作に気付いた... const
キーワードは、オブジェクトを扱うときに不変性を失うようです。JavaScriptの一定の動作ですか?
この現象に関するご意見はありますか?
const fruits = 'banana';
fruit = 'apple';
console.log(fruit); // 'banana' as expected
//----------------------------------------------------------------------
const fruit = ['apple'];
fruit.push('banana');
console.log(fruit); // ['apple', 'banana'] ???????
fruit = 'anything';
console.log(fruit); // ['apple', 'banana'] as expected;
//----------------------------------------------------------------------
const brands = {};
brands = [];
console.log(brands); // {} as expected
brands.sony = "playstation"
console.log(brands); // {sony:'playstation'} ???????
MDNから: 'const宣言は値への読み取り専用参照を作成します。それが保持している値が不変であることを意味するものではなく、単に変数識別子を再割り当てできないということです。それは役に立ちますか? – Mjh
質問をする前に[質問]をお読みください。そのページの最初の見出しは「検索と研究」です。これはすでにインターネット上の多くの場所でカバーされています。 –
あなたはどのブラウザを使用していますか? – ISONecroMAn