在JavaScript中克隆对象 [英] Cloning the object in JavaScript

查看:66
本文介绍了在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.

这篇关于在JavaScript中克隆对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆