[Feature Request] Built-in `deepCopy/deepClone` (based on the structured cloning algorithm)

# Harry Manchanda (6 years ago)

Heya, I tried before to write this but seems like I wasn't subscribed. Now seems like I subscribed so posting it again...

So here we go,

I am posting this after an aftermath of a Github Issue. See =>

tc39/proposals#150

I would like the JavaScript (ECMAScript) Spec to include built-in deepCopy/deepClone feature.

Codepen Example Link: codepen.io/IamManchanda/pen/XYpVPY?editors=0012

Please note that based on the response at Github Issue and also considering that the top 3 npm packages for cloning have over 100k weekly downloads I am very positive on this and should be really considered.

I think we have two approaches to consider for same

  1. Object Oriented approach
// Object.prototype.deepCopy(), Array.prototype.deepCopy(), etc

const harry = {
  name: 'Harry Manchanda',
  age: 25,
  social: [
    {
      website: 'twitter',
      username: '@harmanmanchanda',
    },
    {
      website: 'facebook',
      username: 'IamManchanda',
    },
  ],
};

const dev = harry.deepCopy();
dev.social[1].website = 'github';
  1. Functional Programming approach
// Object.deepCopy(value)
// Value expects array, objects and primitives

const harry = {
  name: 'Harry Manchanda',
  age: 25,
  social: [
    {
      website: 'twitter',
      username: '@harmanmanchanda',
    },
    {
      website: 'facebook',
      username: 'IamManchanda',
    },
  ],
};

const dev = Object.deepCopy(harry);
dev.social[1].website = 'github';

Reason why I think so strongly that everyone in javascript community needs this is this

=> gist.github.com/search?l=JavaScript&q=deepcopy => gist.github.com/search?l=JavaScript&q=deepclone => search?l=JavaScript&q=deepcopy&type=Repositories => search?l=JavaScript&q=deepclone&type=Repositories

Thanks,

Harman Singh Manchanda ( Harry ) Open Source Team Member - Zurb Foundation foundation.zurb.com/get-involved/yetinauts.html

# Jordan Harband (6 years ago)