3 ms·
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 ob
by rmrfrmrf 11y ago
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.
- madflame991 11y ago...and it gets even more interesting if you throw in a few circular references in there
- kolodny 11y agohttps://github.com/kolodny/jsan https://github.com/kolodny/jsan ;)