Object 因為 Call by sharing 的特性,無法直接用 =
複製 Object,今天來講解 Object 的幾種複製方式。
懶人包 正常Object(單層): ...
Nested Object(多層):Lodash cloneDeep
何謂shallow copy(淺複製) 因Javascript特性對於Object會是call by reference,直接用 =
會是shallow copy1 2 3 4 5 6 a = { b :1 , c :2 } copy = a copy.b = 100 console .log (a.b )
copy用法差異 Method Pros Cons = 直接明瞭,預設用法 shallow copy(淺複製) JSON.stringify() and JSON.parse() deep copy nested objects(多層Object) 不能複製物件型別(Function, Date) Object.assign() deep copy 第一層 不能deep copy nested objects … spread operator 同上,語法簡單,複製單層object首選 不能deep copy nested objects Lodash cloneDeep 複製nested objects包含functions 外部package依賴 structuredClone() deep copy nested objects(多層Object) 僅支持可結構化的變數,遇到 Error 和 Function 會拋出 DOMException
=
1 2 3 4 5 6 let john = {name : 'John' , age : 28 }let ken = johnken.name = 'ken' console .log (john.name )
JSON.stringify() and JSON.parse() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 let john = { name : 'John' , age : 28 , job : () => 'Web Developer' , address : { city : 'taipei' } } let ken = JSON .parse (JSON .stringify (john))ken.name = 'ken' ken.address .city = 'new taipei' console .log (john.name ) console .log (john.address .city ) console .log (ken.job )
Object.assign() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 let john = { name : 'John' , age : 28 , job : () => 'Web Developer' , address : { city : 'taipei' } } let ken = Object .assign ({}, john)ken.name = 'ken' ken.address .city = 'new taipei' console .log (john.name ) console .log (ken.job ()) console .log (john.address .city )
...
spread operator1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 let john = { name : 'John' , age : 28 , job : () => 'Web Developer' , address : { city : 'taipei' } } let ken = {...john}ken.name = 'ken' ken.address .city = 'new taipei' console .log (john.name ) console .log (ken.job ()) console .log (john.address .city )
Lodash cloneDeep
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 let john = { name : 'John' , age : 28 , job : () => 'Web Developer' , address : { city : 'taipei' } } let ken = _.cloneDeep (john)ken.name = 'ken' ken.address .city = 'new taipei' console .log (john.name ) console .log (john.address .city ) console .log (ken.job ())
structuredClone() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 let john = { name : 'John' , age : 28 , birthday : new Date ('2022-08-08' ), address : { city : 'taipei' } } let ken = structuredClone (john)ken.name = 'ken' ken.address .city = 'new taipei' ken.birthday .setDate ('30' ) console .log (john.name ) console .log (john.address .city ) console .log (john.birthday .toDateString ()) console .log (ken.name ) console .log (ken.address .city ) console .log (ken.birthday .toDateString ()) let amy = { job : () => 'designer' } structuredClone (amy)
參考