Passing Objects

When you copy an object or pass it to a function, you only pass a reference to that object. If you make a change to the reference, it will modify the original object.

In the below example assigning an object to another variable and then make a change to the copy results in the change of the original object.

var original = {howmany: 1};

var copy = original;

copy.howmany
1

copy.howmany = 100;
100

original.howmany
100

The same thing applies when passing objects to functions.

var original = {howmany: 100};

var nullify = function(o) {o.howmany = 0;}
nullify(original);

original.howmany
0
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +