如何区分JS对象的深拷贝与浅拷贝?

更新于
2026-09-26 12:33:49
1阅读来源:SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计750个文字,预计阅读时间需要3分钟。

如何区分JS对象的深拷贝与浅拷贝?

目录

一、浅拷贝

1.Object.assign(target, source, source...)

如何区分JS对象的深拷贝与浅拷贝?

2.扩展运算符(spread)

二、深拷贝

1.使用对象序列化

JSON.stringify() 和 JSON.parse()

2.使用递归,对对象属性进行判断

一、浅拷贝

1.Object.assign(target, source, source...)

- 将所有可枚举属性的值从一个或多个源对象复制到目标对象,然后返回目标对象。

2. 扩展运算符(spread) - 使用扩展运算符可以将一个数组或对象展开到另一个数组或对象中。

目录
  • 一、浅拷贝
    • 1、Object.assign(target,source,source...)
    • 2、扩展运算符(spread)
  • 二、深拷贝
    • 1、使用对象序列化 JSON.stringify()和JSON.parse()
    • 2、使用递归,对对象属性进行判断

一、浅拷贝

1、Object.assign(target,source,source...)

a、可支持多个对象复制

b、如果source和target属性相同 source会复制target的属性

c、target只能为Object对象

var obj = {a:1,b:2} undefined Object.assign({c:3},obj) {c: 3, a: 1, b: 2} obj {a: 1, b: 2} 兼容性写法if(Object.assign){//兼容}else{//不兼容}

2、扩展运算符(spread)

支持将多个对象复制到一个对象上“

var obj1 = { foo: "foo" }; var obj2 = { bar: "bar" }; var copySpread = { ...obj1, ...obj2 }; // Object {foo: "foo", bar: "bar"} copySpread {foo: "foo", bar: "bar"} var obj = {a:1,b:2,c:3} var objs = {...obj} objs {a: 1, b: 2, c: 3} objs.a=10 10 objs {a: 10, b: 2, c: 3} obj {a: 1, b: 2, c: 3}

二、深拷贝

1、使用对象序列化 JSON.stringify()和JSON.parse()

注意:此方法仅在原对象包含可序列化值类型且没有任何循环引用时才有效。不可序列化值类型的一个例子是Date对象 -JSON.parse只能将其解析为字符串而无法解析回其原始的Date对象 或者对象中属性值为function

var obj = {a:1,b:[1,2,3],c:{e:3},bool:false} undefined var objs = JSON.parse(JSON.stringify(obj)) undefined objs {a: 1, b: Array(3), c: {…}, bool: false} objs.bool = true true objs {a: 1, b: Array(3), c: {…}, bool: true} obj {a: 1, b: Array(3), c: {…}, bool: false}

2、使用递归,对对象属性进行判断

function deepClone(obj) { var copy; // 如果 obj 是 null、undefined 或 不是对象,直接返回 obj // Handle the 3 simple types, and null or undefined if (null == obj || "object" != typeof obj) return obj; // Handle Date if (obj instanceof Date) { copy = new Date(); copy.setTime(obj.getTime()); return copy; } // Handle Array if (obj instanceof Array) { copy = []; for (var i = 0, len = obj.length; i < len; i++) { copy[i] = clone(obj[i]); } return copy; } // Handle Function if (obj instanceof Function) { copy = function() { return obj.apply(this, arguments); } return copy; } // Handle Object if (obj instanceof Object) { copy = {}; for (var attr in obj) { if (obj.hasOwnProperty(attr)) copy[attr] = clone(obj[attr]); } return copy; } throw new Error("Unable to copy obj as type isn't supported " + obj.constructor.name); }

以上就是JS对象复制(深拷贝和浅拷贝)的详细内容,更多关于JS的资料请关注自由互联其它相关文章!

本文共计750个文字,预计阅读时间需要3分钟。

如何区分JS对象的深拷贝与浅拷贝?

目录

一、浅拷贝

1.Object.assign(target, source, source...)

如何区分JS对象的深拷贝与浅拷贝?

2.扩展运算符(spread)

二、深拷贝

1.使用对象序列化

JSON.stringify() 和 JSON.parse()

2.使用递归,对对象属性进行判断

一、浅拷贝

1.Object.assign(target, source, source...)

- 将所有可枚举属性的值从一个或多个源对象复制到目标对象,然后返回目标对象。

2. 扩展运算符(spread) - 使用扩展运算符可以将一个数组或对象展开到另一个数组或对象中。

目录
  • 一、浅拷贝
    • 1、Object.assign(target,source,source...)
    • 2、扩展运算符(spread)
  • 二、深拷贝
    • 1、使用对象序列化 JSON.stringify()和JSON.parse()
    • 2、使用递归,对对象属性进行判断

一、浅拷贝

1、Object.assign(target,source,source...)

a、可支持多个对象复制

b、如果source和target属性相同 source会复制target的属性

c、target只能为Object对象

var obj = {a:1,b:2} undefined Object.assign({c:3},obj) {c: 3, a: 1, b: 2} obj {a: 1, b: 2} 兼容性写法if(Object.assign){//兼容}else{//不兼容}

2、扩展运算符(spread)

支持将多个对象复制到一个对象上“

var obj1 = { foo: "foo" }; var obj2 = { bar: "bar" }; var copySpread = { ...obj1, ...obj2 }; // Object {foo: "foo", bar: "bar"} copySpread {foo: "foo", bar: "bar"} var obj = {a:1,b:2,c:3} var objs = {...obj} objs {a: 1, b: 2, c: 3} objs.a=10 10 objs {a: 10, b: 2, c: 3} obj {a: 1, b: 2, c: 3}

二、深拷贝

1、使用对象序列化 JSON.stringify()和JSON.parse()

注意:此方法仅在原对象包含可序列化值类型且没有任何循环引用时才有效。不可序列化值类型的一个例子是Date对象 -JSON.parse只能将其解析为字符串而无法解析回其原始的Date对象 或者对象中属性值为function

var obj = {a:1,b:[1,2,3],c:{e:3},bool:false} undefined var objs = JSON.parse(JSON.stringify(obj)) undefined objs {a: 1, b: Array(3), c: {…}, bool: false} objs.bool = true true objs {a: 1, b: Array(3), c: {…}, bool: true} obj {a: 1, b: Array(3), c: {…}, bool: false}

2、使用递归,对对象属性进行判断

function deepClone(obj) { var copy; // 如果 obj 是 null、undefined 或 不是对象,直接返回 obj // Handle the 3 simple types, and null or undefined if (null == obj || "object" != typeof obj) return obj; // Handle Date if (obj instanceof Date) { copy = new Date(); copy.setTime(obj.getTime()); return copy; } // Handle Array if (obj instanceof Array) { copy = []; for (var i = 0, len = obj.length; i < len; i++) { copy[i] = clone(obj[i]); } return copy; } // Handle Function if (obj instanceof Function) { copy = function() { return obj.apply(this, arguments); } return copy; } // Handle Object if (obj instanceof Object) { copy = {}; for (var attr in obj) { if (obj.hasOwnProperty(attr)) copy[attr] = clone(obj[attr]); } return copy; } throw new Error("Unable to copy obj as type isn't supported " + obj.constructor.name); }

以上就是JS对象复制(深拷贝和浅拷贝)的详细内容,更多关于JS的资料请关注自由互联其它相关文章!