JavaScript in the Clone object detailed Knowledge _ basics

Source: Internet
Author: User
Tags arrays list of attributes

Development, interrupt the reference relationship between objects, just want the next copy of the situation everywhere, clone an object is inevitable.

In JavaScript, the simple way is to use the JSON function, stringify the object into a string, and then parse it into a new object. or from the Internet search code, open source community inside the clone code is still a lot of.

Although the code can be found, but, things are always someone else's, hands-on learning code is always a constant theme.

I wrote two cloned functions:

Cloneown: Clones the own properties of custom objects, excluding inherited properties, attributes can be basic data types and arrays, custom objects, and you can make a list of attribute names to be cloned.

Clonearray: Cloning arrays, the elements within an array can be objects, basic types.

Copy Code code as follows:

The first parameter is the cloned object, and the second parameter is the list of attributes that need to be cloned
function Cloneown () {
var obj = arguments[0];
if (typeof obj = = ' undefined ' | | | obj = = NULL)
return {};
if (typeof obj!== ' object ')
return obj;
The second argument is the list of property names, which is used to brush the selection
Otherwise, all attributes are 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;
}

Copy Code code as follows:

Cloning 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 Cloning Custom objects:

Copy Code code as follows:

var a = {
Name: ' Frank ',
Age:20
};
var b= cloneown (a);

2. Specify the properties of the clone

Copy Code code as follows:

var a = {
Name: ' Frank ',
AGE:20,
Address: ' Any where '
};
var B = Cloneowne (A, [' Name ', ' age ']);

3. Cloning a custom object that contains an array property

Copy Code code 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 (a);

4. Cloning arrays, containing custom objects

Copy Code code as follows:

var a = [
{
Name: ' Frank ',
Age:20
},
{
Name: ' Leon ',
Age:30
}
];
var B = Clonearray (a);

The above code still has a lot of problems, for example, there are some problems with the cloning of built-in objects, such as the datatime type.

Problem management problem, such a learning process is also needed.

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.