JS object Replication

Source: Internet
Author: User

A Web UI plug-in was recently used, but its global variables were used for calling,

That is to say, a page can only be used once. This plug-in is quite useful, but I need more than two pages.

This plug-in makes me think of JS object cloning.

Idea: clone an object (not a reference, reference or the same object), and then

Change the key attributes (such as ID) of the new object to two different objects.

I can modify these two objects on the page without affecting them.

Unfortunately, JS does not have a ready-made clone method. You need to write it yourself.

Some people have written such a method on the Internet, but there may be problems because recursion is required.

I tried it and it was really a problem and it was useless.

However, jquery's extend can be implemented, which has been tested and recorded today.

// Perform shallow copy (only copy the top-level non-object elements)
// Var newobject = jquery. Extend ({}, oldobject );

// Deep replication (one layer to the bottom)
// Var newobject = jquery. Extend (true, {}, oldobject );

 

 

Function clone (o) {If (! O) {return O;} else {var C; If (object. prototype. tostring. apply (o) = '[object array]') {c = []; for (VAR I = 0; I <O. length; I ++) {C. push (clone (O [I]);} // use this judgment, instead of typeof (o) = 'object'. Here, only the object is processed, // do not process other functions, including array, String, date, and function. However, the function object created by new is also an object} else if (object. prototype. tostring. call (o) = '[object]') {c = {}; for (var p in O) {c [p] = clone (O [p]) ;}} else {return O ;} Return C ;}} function clonedd (myobj) {If (typeof (myobj )! = 'Object') return myobj; If (myobj = NULL) return myobj; var mynewobj = new object (); For (var I in myobj) mynewobj [I] = clonedd (myobj [I]); Return mynewobj;} function MyClone (OBJ) {var arr1 = new array (); arr1.push (OBJ ); vaR arr2 = []. concat (arr1); var res = arr2 [0]; return res;} // shallow copy (only copying top-level non-object elements) // var newobject = jquery. extend ({}, oldobject); // deep replication (one layer to the bottom) // var newobject = jquery. extend (true, {}, oldobject );

 

The following figure shows how to use the global variable navtab,

Copy this variable to get A, then change its attribute ID, and OK to complete the replication.

 
$ (Function () {navtab. init ({ID: "navtab"}); var A = jquery. extend (true, {}, navtab);. init ({ID: "navtab2 "});});

 

 

 

 

 

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.