对象深拷贝
Example
js
const obj = {
a: 1,
b: 2,
c: [3, 4, {a: 1, b: 2}]
}
const copyObj = deepCopy(obj);
Framework
js
import { deepCopy } from 'wgl-utils';
// import deepCopy from 'wgl-utils/es/deepCopy.mjs';
Browser
html
<script type="module">
import { deepCopy } from 'wgl-utils/main.mjs';
// import deepCopy from 'wgl-utils/es/deepCopy.mjs';
</script>
html
<!-- 全量引入 -->
<script src="wgl-utils/main.js"></script>
<script>
Wgl.deepCopy();
</script>
Code
js
// src/deepCopy.js
// 深拷贝
export default function deepCopy(obj) {
var result;
// 判断是否是简单数据类型,
if (typeof obj === 'object') {
// 复杂数据类型
result = Array.isArray(obj) ? [] : {};
for (let i in obj) {
result[i] = typeof obj[i] === 'object' ? deepCopy(obj[i]) : obj[i];
}
} else {
// 简单数据类型 直接 == 赋值
result = obj;
}
return result;
}
js
// dist/es/deepCopy.mjs
var e={d:function(r,t){for(var o in t)e.o(t,o)&&!e.o(r,o)&&Object.defineProperty(r,o,{enumerable:!0,get:t[o]})},o:function(e,r){return Object.prototype.hasOwnProperty.call(e,r)}},r={};function t(e){var r;if("object"==typeof e){r=Array.isArray(e)?[]:{};for(let o in e)r[o]="object"==typeof e[o]?t(e[o]):e[o]}else r=e;return r}e.d(r,{Z:function(){return t}});var o=r.Z;export{o as default};
//# sourceMappingURL=deepCopy.mjs.map