Hacker News new | past | comments | ask | show | jobs | submit login

You can make an interesting challenge out any one of the bigger (in LOC) functions of underscore.js or any promise library. You get a profound "a-ha" moment the first time you implement `deepClone` or `Promise.all`



I'm not super familiar with javascript, but are all copies shallow by default? Deep copies are trivial enough to bake into the language.


JavaScript objects are always pass reference by value (i.e. you're passing around addresses copied by value which are then dereferenced when needed). Copying objects at all in JavaScript is therefore somewhat non-trivial, especially if you need good performance. Typically, the most performant way to deep-copy an object with an unknown structure is to do:

    var objCopy = JSON.parse(JSON.stringify(obj));
But that 1) is a rather disgusting hack and 2) can potentially break on certain native JS Objects (like Date, for example). The most straightforward way would be to use a for-in loop with a guard for Object.hasOwnProperty(), but in practice, that's very slow by comparison.


...and it gets even more interesting if you throw in a few circular references in there





Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: