Home > Web Front-end > JS Tutorial > body text

Instructions for using javascript copy objects_js object-oriented

WBOY
Release: 2016-05-16 18:05:32
Original
925 people have browsed it

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>





Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!