I'm horrid with proposals, but this seems like a missing hole and I
couldn't find a previous mention of it.

Looking at the Object support methods it seems that the only thing that
doesn't exist is a (simple) way to clone an object and all of its members.

Ideally would allow creation of immutable and/or deep/shallow clones of
existing Object/Array/RegExp/Date/...

Basic shim (most likely not perfect):

Object.clone = function(src){
  if(!src || typeof(src) !== 'object'){
    return src;
  }
  if(src instanceof Date){
    return new Date(src);
  }
  if(src instanceof Array){
    return src.map((item)=>clone(item));
  }
  if(src instanceof RegExp){
    return new RegExp(src);
  }
  var temp = (src.prototype)?Object.create(src.prototype):new
src.constructor();
  Object.keys(src).forEach((key)=>{temp[key] = Object.clone(src[key]);});
  return temp;
};

Thoughts?
 - Jeremy
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to