Upload
boy-lee
View
58
Download
1
Embed Size (px)
Citation preview
JS Basic: By Value V.S By Reference
Leeboy @ goodideas-studio2017.03.10
Primitive type(1/6)
Booleans
Number
String
undefined
null
compare by value ⽤值相比
只有在具有相同的值,兩者才相等
Pass by value (e.g 1)
Copy by value (e.g 2)
Primitive type(2/6)e.g.1 Pass by value
Primitive type(3/6)e.g.2 Copy by value
Primitive type(4/6)String
- 不可變的(immutable)(e.g 3)
- 長度相等,索引位置字元相等 👉Quiz
- Are strings compared by value or by reference?
Primitive type(5/6)immutable
Quiz1Q:寫⼀個比較兩字串的函數
Primitive type(6/6)summary
Objective type(1/4)
Object
Function
Array
Set
mutable
compare (e.g1)(e.g 2)
個別物件永不相等
個別陣列永不相等
clone 如何複製 (e.g3)(👉quiz)
Objective type(2/4)e.g.1 Compare by reference
Two references to distinct values are not equal, even if the two values consist of the same bytes.
Objective type(3/4)e.g.2 Compare by reference
Two references are compared to see if they refer to the same value.
Objective type(4/4)e.g.3 Object Clone
Quiz2 Array Clone
請複製 const DC = ['Batman', 'Jocker', 'Flash', 'SuperGirl']
DC_copy.push(‘Arrow') //在複製的陣列 push ⼀個新值
console.log(DC,DC_copy) // 印在 console