Skip to content

Latest commit

 

History

History
30 lines (19 loc) · 848 Bytes

shallow-clone.md

File metadata and controls

30 lines (19 loc) · 848 Bytes
标题 标签
shallowClone(浅复制对象) object(对象)

创建对象的浅克隆。

  • 使用 Object.assign() 和空对象 ({}) 创建原始对象的浅表克隆。
const shallowClone = obj => Object.assign({}, obj);

调用方式:

const a = { x: true, y: 1 };
const b = shallowClone(a); // a !== b

应用场景

以下是一个实战示例:

结果如下:

<iframe src="codes/javascript/html/shallow-clone.html"></iframe>