javascript clone object

ioustinos_sarris
ioustinos_sarris used Ask the Experts™
on
Hi there, i am looking for a way to clone objects. I have found variations of clonig methods but all seem to be questioned by others...Could anyone suggest a sure and stable clone method?
Note that i need DEEP cloning...

Moreover, if i want to simplify things and know beforehand which objects i need to be cloning, can't i just create a property function in these objects? what would that be?

thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
HonorGodSoftware Engineer

Commented:
Are you talking about document elements?

http://www.java2s.com/Code/JavaScriptReference/Javascript-Methods/cloneNode.htm

There is a cloneNode method...
Pravin AsarPrincipal Systems Engineer
Top Expert 2005

Commented:
Can you please explain what you mean by "DEEP cloning"


Here is a example which can clone entire table

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<script language="javascript">
function UpdateTable (tableId, divId) {
var tObj = document.getElementById (tableId);
var dObj = document.getElementById (divId);
var nObj = tObj.cloneNode (true);
dObj.appendChild (nObj);
tObj.parentNode.removeChild (tObj);
alert (dObj.outerHTML);
}
</script>

<table id="mytable"><tr><td>test</td></tr></table>
<div id="mydiv"></div>
<input type="button" value="Insert Table in Div" onClick="UpdateTable ('mytable'
, 'mydiv')">

</body>
</html>
Software Engineer
Commented:
Deep cloning is when you have a hierarchy of objects, and you want to duplicate the entire structure, not just copy references.

http://www.devx.com/tips/Tip/13625
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Pravin AsarPrincipal Systems Engineer
Top Expert 2005

Commented:
I was referring to second part of the  question

>> Moreover, if i want to simplify things and know beforehand which objects i need to be cloning, can't i just create a property function in these objects?

In object being cloned, he wants to ignore certain child objects ??

 
HonorGodSoftware Engineer

Commented:
parvinasar:  Please accept my apologies.  I didn't notice that it was you who had made the update.

>> Moreover, if i want to simplify things and know beforehand which objects i need to be cloning, can't i just create a property function in these objects?

To my knowledge, no such property, or mechanism exists.
HonorGodSoftware Engineer

Commented:
Thanks for the grade & points.

Good luck & have a great day.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial