在JavaScript中克隆对象
问题描述:
我已经使用以下代码创建了对象
Hi i have use the following code to create the object
var parent = {};
parent["Task name"] = "Task " + ++x;
parent["Start time"] = "01/03/2013";
parent["End time"] = "01/08/2013";
parent["Duration"] = "5 days";
parent["Status"] = Math.round(Math.random() * 100);
如何使用JavaScript代码克隆/获取对象的副本。还有其他方法可以实现此目的吗?
How to clone / take the copy of the object using JavaScript Code . Is there any other way to achieve this?
答
克隆对象的最简单方法是使用以下函数:
The simplest way to clone an object is using the following function:
function clone(a){var b=function(){};b.prototype=a;return new b;}
这将创建对象的基本副本,请注意,虽然这不会创建深层副本,但只会创建浅层副本一个。
This creates a basic copy of the object, do note though that this does not create a deep copy, only a shallow one.