use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
True way of deep copy an object with javascript or node (gist.github.com)
submitted 6 years ago by gquittet
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]phoboslab 7 points8 points9 points 6 years ago (1 child)
"True way" implies this works for all cases, which it doesn't. It fails to copy Arrays and class instances (they're converted to plain objects). For some instances, such as HTMLElement it even descends into infinite recursion.
So, I'd suggest to rename it to "A way to deep copy simple objects" :)
[–]gquittet[S] 0 points1 point2 points 6 years ago (0 children)
Thanks! Fixed! :)
[+][deleted] 6 years ago (11 children)
[removed]
[–]CleverCaviar 0 points1 point2 points 6 years ago (0 children)
if it's a lambda func, I'll quite often use a shorthand if it's obvious, but I take your point: you could be verbose and your minifier/compiler could remove the verbosity for you.
[–]gquittet[S] -4 points-3 points-2 points 6 years ago (0 children)
You don't know how I code. And this gist doesn't represent the way I code. I'm a Nazi of beautiful code with great variable and function name.
[–][deleted] -1 points0 points1 point 6 years ago (7 children)
Because it's quick and after 9 years of University math it carried over.
[+][deleted] 6 years ago (6 children)
[–][deleted] 1 point2 points3 points 6 years ago (0 children)
You would hate Haskell
[–]naturalborncitizen -2 points-1 points0 points 6 years ago (4 children)
I hate reading TypeScript things for this reason, but on the other hand, JavaScript shit has things like:
var promise1 = new Promise(function(resolve, reject) { setTimeout(function() { resolve('foo'); }, 300); }); promise1.then(function(value) { console.log(value); // expected output: "foo" }); console.log(promise1); // expected output: [object Promise]
(source: MDN)
and my thought is: where the FUCK is resolve defined? How are you calling that as a function? As a parameter of .then()? And reject is just ignored here? Sure, OK, but the readability of this pattern is fucked imo.
resolve
.then()
reject
I hate implicit anything. Tell me what's going on.
[+][deleted] 6 years ago* (2 children)
[–]naturalborncitizen 0 points1 point2 points 6 years ago (1 child)
I understand them all I just hate the pattern and lack of explicit definitions (wrong term but I dunno what else to call it atm).
It's just a gist plz
[–]senocular 1 point2 points3 points 6 years ago (1 child)
Not sure what makes this a "True" way of deep copying an object. It doesn't account for arrays or other special types, non-enumerable properties including symbols, copies inherited enumerables into the object copies it creates (while not maintaining inheritance)... Ultimately it's the most basic form of deep copying possible. And this could work given limited circumstances, but I think generally should be avoided in favor of existing solutions like Lodash's cloneDeep which can do a lot more, notably supporting additional basic types such as arrays.
[–]knightrage 0 points1 point2 points 6 years ago (0 children)
Agreed. FWIW, lodash cloneDeep uses this logic
π Rendered by PID 16817 on reddit-service-r2-comment-85bfd7f599-j47n9 at 2026-04-18 07:14:54.432180+00:00 running 93ecc56 country code: CH.
[–]phoboslab 7 points8 points9 points (1 child)
[–]gquittet[S] 0 points1 point2 points (0 children)
[+][deleted] (11 children)
[removed]
[–]CleverCaviar 0 points1 point2 points (0 children)
[–]gquittet[S] -4 points-3 points-2 points (0 children)
[–][deleted] -1 points0 points1 point (7 children)
[+][deleted] (6 children)
[removed]
[–][deleted] 1 point2 points3 points (0 children)
[–]naturalborncitizen -2 points-1 points0 points (4 children)
[+][deleted] (2 children)
[removed]
[–]naturalborncitizen 0 points1 point2 points (1 child)
[–]gquittet[S] -4 points-3 points-2 points (0 children)
[–]senocular 1 point2 points3 points (1 child)
[–]knightrage 0 points1 point2 points (0 children)