# 🏅 5/12 (三) 每日任務 Day 13
###### tags: `Vue 直播班 - 2021 夏季班`
題目 (直接貼上答案)
---
```js=
let name = '小明';
const age = 16;
let person = {
name: '小明',
}
const wallet = {
money: 1000
}
const dessert = ['cake', 'ice-cream'];
/* 請問以下程式碼哪些會報錯? */
name = '小美';
age = 17;
person.name = '大明';
wallet.money = 1100;
wallet = {};
dessert.push('fruit');
dessert = ['chocolate'];
```
回報流程
---
請同學依照下圖教學觀看解答、回報答案:

回報格式如下圖,請在「回報區」使用註解回報答案 (為了統計人數,請同學依序加上「報數」)

c
回報區
---
<!--1: Alivn Chu
age = 17;
wallet = {};
dessert = ['chocolate'];
以上為 const(常數),不可重新宣告
-->
<!--2: Lina Chen
age = 17; // const 不能重新賦予
wallet = {}; // const 不能重新賦予
dessert = ['chocolate']; // const 不能重新賦予
-->
<!--3: Alpha
age = 17; //age為const 不可再次賦值
wallet = {}; //wallet為const 不可再次賦值
dessert = ['chocolate'];//dessert為const 不可再次賦值
-->
<!--4: Yiren
以下會回報錯誤,因為使用 const 建立的變數不能重新賦予值:
age = 17;
wallet = {};
dessert = ['chocolate'];
-->
<!--5: SeanLiu
以下會回報錯誤,因為使用 const 建立的變數不能重新賦予值:
錯誤訊息: Assignment to constant variable.
age = 17;
wallet = {};
dessert = ['chocolate'];
-->
<!-- 6: Vic
// const 不能重新賦予值
age= 17;
wallet = {};
dessert = ['chocolate'];
-->
<!-- 7: Eric-小偉哥
以下三項均會報錯,因為const變數不能重新賦予值:
age = 17;
wallet = {};
dessert = ['chocolate'];
-->
<!--8: 小魚
以下會回報錯誤,因為使用 const 建立的變數不能重新賦予值:
age = 17; //const 不可重新賦予值
wallet = {}; //const 不可重新賦予值
dessert = ['chocolate']; //const 不可重新賦予值
-->
<!-- 9: Jordan Tseng
age = 17;//會錯,const不能重新賦值
wallet = {};//會錯,const不能重新賦值
dessert = ['chocolate'];//會錯,const不能重新賦值
筆記:
物件、陣列:使用 const 宣告好一個常數,再用 push 去新增值時,並不會有錯誤的情形,因為在 JS 中陣列和物件都是 reference type,因此尚未重新賦值,指的都還是同一個記憶體。同理物件用`wallet.money` 的方式改屬性或是新增屬性,也都OK~
*注意:如果是 `wallet = {}`、`dessert = []` 的方式,就不是指向同一個記憶體了,所以會出現錯誤。
-->
<!--10: leolee
error 因為被const的變數不能再次賦予新值
age = 17;
wallet = {};
dessert = ['chocolate'];
理由應該是因為樓上的指向同一記憶體的概念
-->
<!-- 11: Alysa Chan
以下程式碼會報錯,因為const不能被重新賦值:
age = 17;
wallet = {};
dessert = ['chocolate'];
-->
<!-- 12: 圈圈
以下程式碼會報錯:
age = 17;
wallet = {};
dessert = ['chocolate'];
-->
<!-- 13: Tori
以下程式碼因為變數是使用const做宣告,值很難再被重新賦予
因此會報錯:
age = 17;
wallet.money = 1100;
wallet = {};
dessert = ['chocolate'];
-->
<!-- 14: Li Shang
以下重賦值會報錯
age = 17;
wallet = {};
dessert = ['chocolate'];
出錯原因: 用const宣告的變數,不能重新賦值值
以下不會報錯
person.name = '大明';
wallet.money = 1100;
dessert.push('fruit');
原因: 針對變數中的物件內容賦值(重賦值),不會更改到物件的地址
-->
<!-- 15: Ethan
age = 17;
wallet = {};
dessert = ['chocolate'];
常數宣告(const)的變數不可重複指定值也不可重複宣告。
物件是儲存記憶體位置,所以修改內容不會修改變數,故對物件內容修改不會出錯
-->
<!-- 16: Iven
以下為會出錯的程式碼
age = 17; 不能重新指派
wallet = {}; 不能重新指派
dessert = ['chocolate']; 不能重新指派
-->
<!--17: Jasmin
age = 17; //const 不可再次賦值
wallet = {}; //const 不可再次賦值
dessert = ['chocolate']; //const 不可再次賦值
-->
<!--18 peter.chen1024
//宣告const的變數,不可修改
//宣告const的物件,不可重新指向(物件內容可以修改,但指向的位址不行)
age = 17;
wallet = {};
dessert = ['chocolate'];
-->
<!-- 19: WuJungHan
// const 不能重新賦予值
age= 17; //const 不能重新賦予值
wallet = {}; //const 不能重新賦予值
dessert = ['chocolate']; //宣告const的物件,不可重新指向(物件內容可以修改,但指向的位址不行)
引用Jordan Tseng
筆記:
物件、陣列:使用 const 宣告好一個常數,再用 push 去新增值時,並不會有錯誤的情形,因為在 JS 中陣列和物件都是 reference type,因此尚未重新賦值,指的都還是同一個記憶體。同理物件用`wallet.money` 的方式改屬性或是新增屬性,也都OK~
*注意:如果是 `wallet = {}`、`dessert = []` 的方式,就不是指向同一個記憶體了,所以會出現錯誤。
-->
<!-- 20陳sam
//const 不可再次賦值
age=17;//const 不可再次賦值
wallet = {};//const 不可再次賦值
dessert = ['chocolate'];//const的物件,不可重新指向(物件內容可以修改,不影響指向到的記憶體位址,反之不行)
-->
<!--21: Yi Chieh
age = 17; // 報錯,使用 const 宣告不可重新賦予值
wallet = {}; // 報錯,使用 const 宣告不可重新賦予值
dessert = ['chocolate']; // 報錯,使用 const 宣告不可重新賦予值
-->
<!-- 22: Harold
age = 17; // 會,因為const不可以重新派值
wallet = {}; // 會,因為const不可以重新宣告
dessert = ['chocolate']; // 會,因為const不可以重新宣告
-->
<!-- 23: Sec
age = 17 会报错, 因为 const 是不能被reassign value
wallet = {} 会报错, 因为 const 是不能被 reassign value
dessert = ["chocolate"] 会报错, 因为const 是不能被 reassign value
-->
<!--24:yijun
age = 17;
wallet = {};
dessert = ['chocolate'];
以上三個皆會報錯,因為const不能重新賦予值。
-->
<!--25:Chiang
以下程式碼會報錯:
age = 17;
wallet = {};
dessert = ['chocolate'];
因用 const 宣告不能重新賦予值
-->
<!--26: Joe Kuo
age = 17;
wallet = {};
dessert = ['chocolate'];
以上都會報錯 都是const(常數)宣告,不可重新賦予值
-->
<!-- Echo Hui
age = 17;
wallet = {};
dessert = ['chocolate'];
以上都會報錯 , Uncaught TypeError: Assignment to constant variable.
-->
<!--28: shoppingq
https://codepen.io/shoppingq/pen/JjWGjgZ
-->
<!--29: Larry
codePen:
https://codepen.io/manpower0708/pen/ZEeQENy?editors=1011
-->
<!-- 30:wel
name = '小美';
age = 17; //錯誤 const 無法重新賦予值
person.name = '大明';
wallet.money = 1100;
wallet = {}; //錯誤 const 無法重新賦予值
dessert.push('fruit');
dessert = ['chocolate']; //錯誤 const 無法重新賦予值
//補充說明
wallet.money = 1100;
傳參考可以修改物件內的屬性和屬性值。
可參考卡斯伯老師網址:
https://wcc723.github.io/javascript/2017/12/20/javascript-es6-let-const/
-->
<!-- 31:Poseidon
age = 17;
wallet = {};
dessert = ['chocolate'];
-->
<!-- 32: Oober
age = 17;
wallet = {};
dessert = ['chocolate'];
-->
<!-- 33: Jay
age = 17;
wallet = {};
dessert = ['chocolate'];
任務重點:理解 const 的兩個特性,固定的 Data type(變數不能再次賦值)、物件的內容可以被更動
-->
<!-- 34: Jack
age = 17;
wallet = {};
dessert=['chocolate']
-->
<!-- 35: youting
const 宣告的變數不能重新賦予值
const 物件內的值可以更改,因為不是整個重新賦予,只是指向記憶體
age = 17
wallet = {}
dessert=['chocolate']
-->
<!-- 36: Erica
age = 17;
wallet = {};
dessert=['chocolate']
-->
<!-- 37: Ed Huang
name = '小美';
age = 17; // X 因為用 const 宣告變數,是不能重新賦予值
person.name = '大明';
wallet.money = 1100;
wallet = {}; // X 因為用 const 宣告變數,是不能重新賦予值
dessert.push('fruit');
dessert = ['chocolate']; // X 因為用 const 宣告變數,是不能重新賦予值
-->
<!-- 38: Izumi 泉
https://codepen.io/izumi-dev/pen/gOgEZzM
-->
<!-- 39: Echo
name = '小美'; //不會錯
age = 17; //會報錯
person.name = '大明'; //不會錯
wallet.money = 1100; //不會錯
wallet = {}; //會報錯
dessert.push('fruit'); //不會錯
dessert = ['chocolate']; //會報錯
-->
<!-- 40: 阿倫
name = '小美'; //沒報錯
age = 17; //報錯,用Const宣告不能重新指派
person.name = '大明'; //沒報錯
wallet.money = 1100;
wallet = {}; //報錯,用Const宣告不能重新指派
dessert.push('fruit'); //沒報錯
dessert = ['chocolate']; //報錯,用Const宣告不能重新指派
-->
<!-- 41: Ted
age = 17;
wallet = {};
dessert = ['chocolate'];
-->
<!-- 42: Trsunyeng Yu
age = 17; // "const 變數" 無法被重新 assign
wallet = {}; // "const object" 的參考無法被重新 assign
dessert = ['chocolate']; // "const array" 的參考無法被重新 assign
-->
<!-- 43: 涂阿銘
name = '小美'; // 可執行
age = 17; // 錯誤,因為宣告使用 const 所以無法修改其值
person.name = '大明'; // 可執行
wallet.money = 1100; // 可執行,因為物件、陣列的屬性並沒有被保護
wallet = {}; // 錯誤,嘗試覆寫該物件、陣列將會發生錯誤
dessert.push('fruit'); // 可執行,同上
dessert = ['chocolate']; // 錯誤,同上
-->
<!-- 44: Alicia Lo
age=17; //const 不能重新賦值
wallet={}; //const 不能重新賦值新物件
dessert=['chocolate']; //const 不能重新賦值新陣列
-->
<!--45: Dah
const 宣告不能做變更
age = 17;
dessert = ['chocolate'];
wallet = {};
-->
<!-- Wendy Li
/* 請問以下程式碼哪些會報錯? */
name = '小美';
age = 17; // 不可執行, 因為const是常數, 宣告後不能重新賦予值
person.name = '大明'; // 可執行, 物件、陣列屬性沒有被保護
wallet.money = 1100; // 可執行, 物件、陣列屬性沒有被保護
wallet = {}; // 不可執行, 因為const是常數, 宣告後不能重新賦予值
dessert.push('fruit'); // 可執行, 物件、陣列屬性沒有被保護
dessert = ['chocolate']; // 不可執行, 因為const是常數, 宣告後不能重新賦予值
-->
<!--47: Jessie Cheng
age = 17; // const 不能重新賦值
wallet = {}; // const 不能重新賦值
dessert = ['chocolate']; // const 不能重新賦值
-->
<!--48 jimmyFang
以下的程式碼皆會報錯 , const是常數,宣告後不能重新賦予值
age = 17;
wallet = {};
dessert = ['chocolate'];
-->
<!--49: Amanda Chiang
A: 以下三行皆會報錯,因 const 不能被重新賦值
age = 17;
wallet = {};
dessert = ['chocolate'];
-->
<!--50: Jrhung-TSai
//宣告 const 會對於它的值建立一個唯讀的參考。並不是說這個值不可變更,而是這個變數不能再一次指定值。因此以下重新賦值是會報錯誤的
age = 17; //不能重新賦值
wallet = {}; //不能重新覆寫物件本身,物件屬性則可以
dessert = ['chocolate']; //不能重新覆寫陣列本身,增加陣列內容則可以
-->
<!--51: twoz
A: 以下三行皆會報錯,因 const 不能被重新賦值,但物件內的屬性可以
age = 17;
wallet = {};
dessert = ['chocolate'];
-->
<!--52:Fred Cgang
/* 請問以下程式碼哪些會報錯? */
name = '小美'; //OK
age = 17; //const無法重新賦值
person.name = '大明'; //OK
wallet.money = 1100; //OK
wallet = {}; //const無法重新賦值
dessert.push('fruit'); //OK
dessert = ['chocolate']; //const無法重新賦值
-->
<!--53: Stacey Huang
以下會報錯,因為const所宣告的變數不能再次賦值,但物件及陣列的內容則可以修改,因為仍指向同一個記憶體位置
age = 17;
wallet = {};
dessert = ['chocolate']
-->
<!--54: Valerie
/* 請問以下程式碼哪些會報錯? */
//age = 17;
//wallet = {};
//dessert = ['chocolate'];
const宣告無法重新賦值
-->
<!-- 55: Tofu Tseng
const 宣告的變數無法重新賦值,const 宣告的物件也無法重新賦值。
但物件或陣列的內容不能重新覆寫,但可以修改,因為是來自同一個記憶體位置。
/* 請問以下程式碼哪些會報錯? */
age = 17;
wallet = {};
dessert = ['chocolate'];
-->
<!-- 56: allen Chou
let name = '小明';
const age = 16;
let person = {
name: '小明',
}
const wallet = {
money: 1000
}
const dessert = ['cake', 'ice-cream'];
/* 請問以下程式碼哪些會報錯? */
age = 17; //重複宣告 報錯
wallet = {}; //重複宣告 抱錯
dessert = ['chocolate']; //重複宣告 抱錯
-->
<!-- 56: 林晉
/* 以下程式碼會報錯 */
age = 17;
wallet = {};
dessert = ['chocolate'];
-->
<!-- 57: Carol
/* 以下程式碼會報錯 */
age = 17;
wallet = {};
dessert = ['chocolate'];
-->
<!--58: Ming
age = 17;
wallet = {};
dessert = ['chocolate'];
const(常數),不可重新宣告
-->
<!-- 59: Eyan
// const 不能重新賦予值
age= 17;
wallet = {};
dessert = ['chocolate'];
-->
<!-- 60: moitw
// const(常數) 不能重新賦予值,如果是物件和陣列,可修改值內部屬性和值,但不可「整個」覆蓋修改
age= 17;
wallet = {};
dessert = ['chocolate'];
-->
<!-- 61: Emily Hsi
// 以下會報錯,因為const為常數,不能重新賦予值
age = 17;
wallet = {};
dessert = ['chocolate'];
-->
<!-- 62: 李重炫
//宣告為const後,不可重新賦予值
age = 17;
wallet = {};
dessert = ['chocolate'];
-->
<!-- 63: Amber
用 const 建立的變數不能重新賦予值
報錯: Assignment to constant variable.
以下程式碼會報錯:
age = 17;
wallet = {};
dessert = ['chocolate'];
-->
<!-- 63: MM
//宣告為const後,不可重新賦予值
以下會報錯:
age = 17;
wallet = {};
dessert = ['chocolate'];
-->