clone icon indicating copy to clipboard operation
clone copied to clipboard

consider removing hasOwnProperty when cloning objects

Open matthewmueller opened this issue 12 years ago • 2 comments

This check causes issues when you try to clone class instances:

Example:

function Datepicker(cls) {
  if(!(this instanceof Datepicker)) return new Datepicker(cls);
  this.className = cls;
}

Datepicker.prototype.toString = function() {
  return this.className;
};

var datepicker = Datepicker('date')
datepicker.toString() // date
datepicker = clone(datepicker); // { className: "date" };
datepicker.toString() // [object Object]

basically, the toString method on the prototype isn't getting copied over. I also noticed underscore doesn't have this check when run _.clone, so it works as expected.

matthewmueller avatar Feb 08 '13 03:02 matthewmueller

:+1:

wilmoore avatar Jun 06 '13 06:06 wilmoore

we can do:

function clone(source) {
  var out = Object.create(source.__proto__)
  for (...)
}

or something.

jonathanong avatar Mar 02 '14 04:03 jonathanong