mail

JavaScript quirks – Objects by reference
privacy
I recently started developing with NodeJS and noticed something peculiar:
var a = {"test": "moo"}; // Define an object var b = a; // Copy the object b.test = "meh"; alert(a.test); // alert: "meh" // wait- what? b = a makes a reference instead of a copy, doh!
One way to get around this is:
var b = JSON.parse(JSON.stringify(a)) // This is one ugly hack
but it feels.. wrong.. and probably doesn’t work with nested functions and stuff like that.![]()
Related posts:
language
What am I doing?- Nichijou 1. January, 2012
- Mashiroiro Symphony 15. November, 2011
- Yuru Yuri 18. August, 2011
- Hanasaku Iroha 13. July, 2011
Interesting stuff
Archives









