Javascript Object clone()

Description

Javascript Object clone()


//third task/*w  w  w  .  j a  va  2s. c o  m*/

var myObject = {firstName : "Olga", lastName: {beforeWedding : 'Petrova', afterWedding : 'Sidorova'}, age: 32};

function cloneObject(obj) {  
    var newObj = {};  
    for (var prop in obj) {  
        if (typeof obj[prop] == 'object') {  
          newObj[prop] = cloneObject(obj[prop]);  
        } else {
          newObj[prop] = obj[prop];
        }
    } 
    return newObj;  
}

var copy = cloneObject(myObject);
console.log(copy);

Javascript Object clone()

Object.prototype.clone = function clone() {
 /**/*ww w .  j a v  a  2  s. c o m*/
   * @constructor
   */
 function F() {}
 F.prototype = this;
 return new F();
}



PreviousNext

Related