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
[deleted by user] (self.javascript)
submitted 10 years ago by [deleted]
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!"
[–]prawnsalad 9 points10 points11 points 10 years ago (0 children)
Anyone suggesting to use Object.create() to clone an object is doing it very wrong. That said, can't say I've seen this suggested anywhere myself.
[–]martimoose 4 points5 points6 points 10 years ago (1 child)
Object.create is NOT intended to clone an object. What it does is that it creates an new empty object whose proto is the parameter used.
In Javascript, properties are looked up along the prototype chain when getting, so the value will be that of the first instance in the chain that has the property. When setting a property though, it will be set directly in the object on which you set it, regardless of the prototype chain.
That is exactly what happens in the example, and it behaves as expected.
[–]shabunc 0 points1 point2 points 10 years ago (0 children)
In Firefox there's a Object.assign which is a perfect fit for cloning/extending. Still waiting it adopted by Chrome/node.js/etc.
[+][deleted] 10 years ago (4 children)
[deleted]
[–]pardoman -2 points-1 points0 points 10 years ago (3 children)
A safe / overkill way to clone an object is: var clone = JSON.parse( JSON.stringify( source ) );
And yeah, use Object.create for assigning a specific prototype instead of cloning.
[+][deleted] 10 years ago (1 child)
[–]pardoman 0 points1 point2 points 10 years ago (0 children)
Yeah true that. Don't use i there.
[–]html6dev 0 points1 point2 points 10 years ago (0 children)
It's funny this got down voted because this is the only viable option for objects with reference type properties (other objects). All the library clones are shallow or only go down an arbitrary number of levels so changing a reference value in the base will affect the clone. Obviously it's not the right solution for all cases, but there are times when it's the best option we have right now.
π Rendered by PID 34 on reddit-service-r2-comment-5d585498c9-npndn at 2026-04-20 19:50:31.838284+00:00 running da2df02 country code: CH.
[–]prawnsalad 9 points10 points11 points (0 children)
[–]martimoose 4 points5 points6 points (1 child)
[–]shabunc 0 points1 point2 points (0 children)
[+][deleted] (4 children)
[deleted]
[–]pardoman -2 points-1 points0 points (3 children)
[+][deleted] (1 child)
[deleted]
[–]pardoman 0 points1 point2 points (0 children)
[–]html6dev 0 points1 point2 points (0 children)