# call by ... 家族 ## 資料型別介紹 資料型別(data type)主要分成兩大類,原始型別(primitive type)和物件(Object) 原始型別: * Boolean * Null * Undefined * Number * BigInt * String * Symbol (ES6才新增的) 補充: ``` BigInt->當number大於2的53次方後會失準所以要使用bigint來解決數值過大的問題 Symbol->在object裡使用,每一個symbol都是獨一無二的,解決object容易出現重複名稱的問題 ``` 物件: * array * function * map * object * ... 重點: **primitive type會是call by value,而object則是call by reference** ## call by ... 介紹 ### Call By Value ``` 1. 傳值呼叫 2. 記憶體位置不共用 3. 只傳值不傳址 ``` 實例 ``` function fn (a, b) { var temp = 100; a = temp; b = a; } var x = 10; var y = 20; fn(x, y); console.log(x, y); // 10, 20 ``` ### Call By Reference ``` 1. 傳參數呼叫 2. 記憶體位置共用 3. 傳值也傳址 ``` 實例 ``` function fn (a) { var temp = 100; a.x = temp; a.y = a.x; } var obj = { x: 10, y: 20, } fn(obj); console.log(obj); // { x: 100, y: 100 } ``` ### Call By Sharing ``` 1. 傳共享物件呼叫 2. 記憶體位置不共用 3. 不傳值也不傳址 ``` 實例 ``` function fn (a) { a = { b: 100, c: 50 } } var obj = { x: 10, y: 20, } fn(obj); console.log(obj); { x: 10, y: 20 } ``` 示範: https://playcode.io/javascript ``` function changeAge(person) { person.age = 25; person = { name: 'John', age: 50 } return person } var personObj1 = { name: 'Charles', age: 30 }; var personObj2 = changeAge(personObj1); console.log(personObj1); console.log(personObj2); ``` 上面是第一種講法 下面有第二種 參考資料 1. https://ithelp.ithome.com.tw/m/articles/1027046 2. https://pjchender.dev/javascript/js-symbols/ 3. https://israynotarray.com/javascript/20200904/1772972600/ 4. https://medium.com/@mengchiang000/js%E5%9F%BA%E6%9C%AC%E8%A7%80%E5%BF%B5-call-by-value-%E9%82%84%E6%98%AFreference-%E5%8F%88%E6%88%96%E6%98%AF-sharing-22a87ca478fc 5. https://blog.techbridge.cc/2018/06/23/javascript-call-by-value-or-reference/