During development, it is inevitable that you can interrupt the reference relationship between objects and just want to leave a copy.
In JavaScript, the simple method is to use the JSON function to stringify the object into a string, and then parse it into a new object. Either search for code online, there are still a lot of clone codes in the open source community.
Although the code can be found, things are always someone else's, and learning codes will always be an unchanging topic.
I wrote two cloned functions myself:
cloneOwn: clone the own properties of a custom object, which does not include inherited properties. The properties can be basic data types and arrays. Custom objects can be customized. You can formulate a list of attribute names to clone.
cloneArray: clone an array, the elements in the array can be objects, basic types.
The code copy is as follows:
//The first parameter is the cloned object, and the second parameter is the attribute list that needs 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, so use this list to select
// 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] instance of 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 copy is as follows:
//Clone the array
function cloneArray(array) {
if (typeof array === 'undefined' || array === null)
return [];
if (!(array instance of 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. Regularly clone custom objects:
The code copy is as follows:
var a = {
name:'frank',
age:20
};
var b= cloneOwn(a);
2. Specify the cloned properties
The code copy 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 copy 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(a);
4.Clone the array with custom objects
The code copy is as follows:
var a = [
{
name:'frank',
age:20
},
{
name:'leon',
age:30
}
];
var b = cloneArray(a);
There are still many problems with the above code, such as cloning of built-in objects, such as the datatime type.
Problems are managed, and such a learning process must also be done.