Object.assign
、 扩展运算符 ...
、 Array.prototype.slice()
、 Array.prototype.concat()
等let obj = { name : ‘Michael‘, age : 18 , others : { hobbies : [‘basketball‘,‘gambling‘], team : ‘Bulls‘, }, } let [newObj,newObj2] = [Object.assign({},obj),{...obj}] obj.name = ‘Kobe‘; obj.others.team = ‘Lakers‘; console.log(obj)
console.log(newObj)
console.log(newObj2)
浅拷贝当第一层的属性值是基本数据类型时,新的对象和原对象互不影响,但是如果第一层的属性值是引用数据类型时,那么新对象和原对象的属性值其指向的是同一块内存地址。
RegExp
或者 Date
类型,返回对应类型出门有事,回来继续写。。。。。
原文:https://www.cnblogs.com/dajuyiding/p/11367883.html