Prototypical inheritance for cloned object - IE10

95 Views Asked by At

I’m trying to clone an object, using lodash’ _.clone.

However, I want to keep the prototypical inheritance intact for the cloned object. IE 10 is not letting me access __proto__ or even this Object.setPrototypeOf(toObj, Object.getPrototypeOf(fromObj)); and I don’t want to access via call or apply on parent object as there are lot of setter and getter method on parent which need to be called from the clone object.

Any suggestion?

1

There are 1 best solutions below

0
On

After some try, I found this one of my use:

/**
 * Shallow clone an object and retains the prototype chain
 * @param   {Object} fromObj Object to be cloned
 * @returns {Object} Cloned Object
 */
function cloneObj(fromObj) {
  var toObj, i;

  if (fromObj && typeof fromObj === 'object') {
    toObj = new fromObj.constructor();

    for (i in fromObj) {
      if (fromObj.hasOwnProperty(i)) {
        toObj[i] = fromObj[i];
      }
    }
  } else {
    throw new Error(fromObj + ' cannot be cloned');
  }

  return toObj;
}