Javascript copy object instructions

Source: Internet
Author: User

OldObj is an object, not a value
For example, var newObj = oldObj;
If you want to change the oldObj, newObj is not affected;
You can write a function like this. Copy codeThe Code is as follows: function clone (myObj ){
If (typeof (myObj )! = 'Object') return myObj;
If (myObj = null) return myObj;
Var myNewObj = new Object ();
For (var I in myObj)
MyNewObj [I] = clone (myObj [I]);
Return myNewObj;
}

Then, call
NewObj = clone (oldObj)
After that, the value of oldObj will not affect newObj no matter how it is changed.

JavaScript copying objects
Syntax:
OElement = object. cloneNode (bCloneChildren)

Parameters:
BCloneChildren: Optional. Boolean ). False | true
False: default value. When cloning an object, the childNodes set of the object is not included. That is, all its sub-objects.
True: When cloning an object, it includes the childNodes set of the object. That is, all its sub-objects.

Return Value:
OElement: Object (Element ). Returns the reference of the new object after cloning.
Note:
Clone an object in the document structure.
After cloning, a collection is returned when the id of the cloned object is obtained.
You can use this method at runtime. Before the close tag of an object is resolved, the file space may not be presented.
Sample Code:Copy codeThe Code is as follows: <script>
Function rdl_fnClone (){
Var oCloneNode = oList. cloneNode (true );
CloneArea. appendChild (oCloneNode );
}
</Script>
<Table width = 98%> <tr> <td>
<Ul id = oList>
<Li> 1st list items
<Li> 2nd <a href = "#" onclick = "return false;"> list items </a>
<Li style = "font-weight: bold;"> 3rd list items
</Ul>
</Td> <td id = cloneArea>
</Td> </tr> </table>
<Input type = button value = "clone" onclick = "rdl_fnClone ();">

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.