Clone object in javascript

  • 2020-03-30 04:26:29
  • OfStack

  In development, where the reference relationship between objects is broken and the desire for the next copy is ubiquitous, clone is inevitable.

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

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

  I wrote two cloned functions:

  CloneOwn: cloneOwn: cloneOwn: cloneOwn: cloneOwn: cloneOwn: cloneOwn: cloneOwn: cloneOwn: cloneOwn: cloneOwn: cloneOwn: cloneOwn: cloneOwn: cloneOwn: cloneOwn: cloneOwn: cloneOwn: cloneOwn: cloneOwn: cloneOwn: cloneOwn: cloneOwn.

  CloneArray: cloneArray cloneArray: cloneArray cloneArray: cloneArray cloneArray: cloneArray.


//The first parameter is the cloned object, and the second parameter is the list of properties 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 parameter is the list of property names, which is used to brush
  //Otherwise clone all properties
  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;
}


//Clone 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. Regular cloning of custom objects:


var a = {
    name:'frank',
    age:20
};
var b= cloneOwn(a);

2. Specify the clone's properties


var a = {
    name:'frank',
    age:20,
    address:'any where'
};
var b = cloneOwne(a, ['name', 'age']);

3. Clone a custom object with array attributes


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 an array that contains custom objects


var a = [
   {
      name:'frank',
      age:20
    },
    {
       name:'leon',
       age:30
     }
];
var b = cloneArray(a);

There are a number of problems with the above code, such as cloning of built-in objects, such as the datatime type.

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


Related articles: