废话不多说了,直接给大家贴代码了,具体代码如下所示:
function mottoClone (obj) { if (obj === null || typeof obj !== 'object') return obj; if (obj instanceof Boolean) return new Boolean(obj.valueOf()); if (obj instanceof Number) return new Number(obj.valueOf()); if (obj instanceof String) return new String(obj.valueOf()); if (obj instanceof RegExp) return new RegExp(obj.valueOf()); if (obj instanceof Date) return new Date(obj.valueOf()); var cpObj = obj instanceof Array "htmlcode">var obj = { id: 1, name: 'xxx', sayName: function () { console.log('my name is' + this.name); }, childs: [ {}, {}, ... ], opts: { xxx: [], ... }, ... }; var newObj = mottoClone(obj);以上所述是小编给大家介绍的JS数据类型的值拷贝函数(深拷贝),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!