Object.clone - not quite a proposal

Jeremy Darling jeremy.darling at gmail.com
Thu Sep 24 16:15:27 UTC 2015

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
  Object.keys(src).forEach((key)=>{temp[key] = Object.clone(src[key]);});
  return temp;

 - Jeremy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150924/757b5ce8/attachment.html>

More information about the es-discuss mailing list