

Deep cloning an object in JS
source link: https://gist.github.com/EkaterinaaTM/e7fd61be33d4a851131e7933b5a26cc4
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Instantly share code, notes, and snippets.
var object_create = Object.create; if (typeof object_create !== 'function') { object_create = function(o) { function F() {} F.prototype = o; return new F(); }; } function deepClone(src) { if(src === null || typeof(src) !== 'object'){ return src; }
//Honor native/custom clone methods if(typeof src.clone == 'function'){ return src.clone(true); }
//Special cases: //Date if(src instanceof Date){ return new Date(src.getTime()); } //RegExp if(src instanceof RegExp){ return new RegExp(src); } //DOM Element if(src.nodeType && typeof src.cloneNode == 'function'){ return src.cloneNode(true); }
//Array if (Object.prototype.toString.call(src) == '[object Array]') { //[].slice() by itself would soft clone var ret = src.slice();
var i = ret.length; while (i--) { ret[i] = deepCopy(ret[i]); } return ret; }
//If we've reached here, we have a regular object var proto = (Object.getPrototypeOf ? Object.getPrototypeOf(src): src.__proto__); var dest = object_create(proto); for (var key in src) { //Note: this does NOT preserve ES5 property attributes like 'writable', 'enumerable', etc. dest[key] = deepCopy(src[key]); } return dest; }
Recommend
-
19
-
38
Guest Author : Sander Stad ( @sqlstad ) PSDatabaseClone is a PowerShell module that has the ability to create images of databases ("clones") and distribute those cl...
-
52
Learn how to copy elements from one array to another using the provided functions in System.Array class. An array in C# is a collection of data items, all of the same type and accessed using a numeral index. The...
-
33
We started Smartcar a few years ago with a powerful mission: making it possible for developers to easily build apps for cars. We created a standard API for cars. Developers can read our docs and use our API to locate or e...
-
84
README.md Real-Time Voice Cloning This repository is an implementation of Transfer Learning from Speaker...
-
31
Understand how celery works by building a clone.(15 July 2019) Intro A delayed job processor(also called a background processor, asynchronous task queue etc) is a software system...
-
22
Real-Time Voice Cloning This repository is an implementation of Transfer Learning from Speaker Verification to Multispeaker Text-To-Speech Synthesis (SV2TTS)...
-
11
Cloning failing disks the right way When you work technical support, you tend to see only the problem machines. This is selection bias in its most obvious form, since happy hardware tends to just sit there and run. From your per...
-
5
Even when you pass an object to a function or method, you are passing this object by reference, not the value. If you pass (or copy) an object by reference and then change any property, the ‘source’ object’s property also changes. In...
-
2
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK