數組
1:數組用concat
let a=[1,2,3] let b=a.concat() a[0]=1000 console.log(a) console.log(b)
值為:
[1000, 2, 3]
[1, 2, 3]
2:數組用Object.assign()
let a=[1,2,3] let b=Object.assign([],a) a[0]=1000 console.log(a) console.log(b) 值為: [1000, 2, 3] [1, 2, 3]
對象
對象用Object.assign()
let a={age:12,name:'寅春樹'}
let b=Object.assign({},a)
a.age=50
console.log(a)
console.log(b)
值為:
{age: 50, name: "寅春樹"}
{age: 12, name: "寅春樹"}
殺手鐧:數組中有對象
let a=[1,2,3,{age:12,name:"寅春樹"}]
let b=JSON.parse(JSON.stringify(a))
a[0]=100
a[3].age=50
console.log(a)
console.log(b)
值為:

