Home >Web Front-end >JS Tutorial >Instructions for using javascript copy objects_js object-oriented

Instructions for using javascript copy objects_js object-oriented

WBOY
WBOYOriginal
2016-05-16 18:05:32949browse

oldObj is an object, not a value
For example, var newObj=oldObj;
If you want oldObj to change without affecting newObj;
You can write a function like this

Copy code The 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, no matter how the value of oldObj changes, it will not affect newObj

JavaScript copy object
Syntax:
oElement = object . cloneNode ( bCloneChildren )

Parameters:
bCloneChildren: Optional. Boolean. false | true
false : Default value. When cloning an object, it does not include the object's childNodes collection. That is, all its child objects.
true : Include object’s childNodes collection when cloning object. That is, all its child objects.

Return value:
oElement : Object (Element). Returns a reference to the new cloned object.
Description:
Clone object in the document structure.
After cloning, when getting the id of the cloned object, a collection will be returned.
It is OK to use this method at runtime. The document space may not be rendered until the object's closing tag is parsed.
Sample code:
Copy code The code is as follows:

<script> <br>function rdl_fnClone(){ <br>var oCloneNode=oList.cloneNode(true); <br>cloneArea.appendChild(oCloneNode); <br>} <br></script>





Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn