[JavaScript] deep and shallow clone clone


var originObj = {
  name: "小明",
  array:["666","777","888"],
  object:{
    name:"lgq",
    prop:["good","beautiful","cool"],
    obj:{O
      age:"18",
      sex:"boy"
    }
  }
}

var targetObj = {};

 

/ **
* shallow clone
* /

function clone(origin,target){

  var target = target || {}; // target if defined, the default is} {

  for (var prop in Origin) {// attributes enumerated original object (the same applies only prop array is an array subscripts 0,1, ... 2)
    iF (origin.hasOwnProperty (prop)) {// determines whether the attribute is a property of its own return true or false (its properties in order to filter the prototype chain)
      target [prop] = Origin [prop] // clone the original object property to audiences
    }
  }

  return target;
}

clone(originObj ,targetObj ) //执行浅clone

Guess you like

Origin www.cnblogs.com/arvin-LGQ/p/11319537.html