Detailed description of clone objects in javascript _ basic knowledge-js tutorial

Source: Internet
Author: User
Tags list of attributes
This article mainly introduces how to clone objects in javascript. If you need a friend, you can refer to the reference relationship between objects under development and interrupt them, but only the next copy is everywhere, it is inevitable to clone an object.

In JavaScript, the simple method is to use the JSON function to convert the object stringify into a string, and then parse into a new object. You can either search for code from the Internet, and there are still a lot of clone code in the open-source community.

Although the Code can be found, things are always others', and learning code is always a constant topic.

I wrote two clone functions myself:

CloneOwn: clone a custom object's own attributes, excluding inherited attributes. attributes can be basic data types and arrays, custom objects, and a list of attributes to be cloned.

CloneArray: clone an array. The elements in the array can be objects and basic types.

The Code is as follows:


// The first parameter is the cloned object, and the second parameter is the list of attributes to be cloned.
Function cloneOwn (){
Var obj = arguments [0];
If (typeof obj = 'undefined' | obj = null)
Return {};
If (typeof obj! = 'Object ')
Return obj;
// The second parameter is the attribute name list, which is used for selection.
// Otherwise, all attributes will be cloned.
Var attrs = arguments [1];
Var enable_spec_attr = true;
If (! (Attrs instanceof Array )){
// Console. log (attrs );
Attrs = obj;
Enable_spec_attr = false;
}
Var result = {};
Var I;
For (I in attrs ){
Attr = enable_spec_attr? Attrs [I]: I;
// Console. log (attr );
If (obj. hasOwnProperty (attr )){
If (obj [attr] instanceof Array ){
Result [attr] = cloneArray (obj [attr]);
}
Else if (typeof obj [attr] === 'object '){
Result [attr] = cloneOwn (obj [attr]);
} Else {
Result [attr] = obj [attr];
}
}
}
Return result;
}

The Code is as follows:


// Clone an array
Function cloneArray (array ){
If (typeof array = 'undefined' | array = null)
Return [];

If (! (Array instanceof Array ))
Return [];

Result = [];

Var I;
For (I in array ){
If (typeof array [I]! = 'Object '){
Result [I] = array [I];
Continue;
}

// Clone object
Result [I] = cloneOwn (array [I]);
}

Return result;
}

Call

1. General clone custom object:

The Code is as follows:


Var a = {
Name: 'frank ',
Age: 20
};
Var B = cloneOwn ();

2. Specify clone attributes

The Code is as follows:


Var a = {
Name: 'frank ',
Age: 20,
Address: 'Any where'
};
Var B = cloneOwne (a, ['name', 'age']);

3. clone a custom object containing array attributes

The Code is as follows:


Var a = {
Name: 'kxh ',
Age: 20,
Books: ['hai ', 'ho', 'Ali'],
Likes :[
{Wname: 'kaili', wage: 81, fav: "aaaaa "},
{Wname: 'seli', wage: 82, fav: "bbb "},
{Wname: 'ailun ', wage: 83, fav: "ccc"},]
};
Var B = cloneOwne ();

4. Clone an array containing custom objects

The Code is as follows:


Var a = [
{
Name: 'frank ',
Age: 20
},
{
Name: 'on ',
Age: 30
}
];
Var B = cloneArray ();

There are still many problems with the above Code. For example, the clone of built-in objects has some problems, such as the datatime type.

Problem management, such a learning process is also necessary.

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.