With a = {}; b = a, you get
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
a \ \ { } / / b Then with a['one'] = {} you get a \ \ { one: { } } / / b Then with a = a['one'] you get a - - - - \ { one: { } } / / b |
quoted from stackoverflow:
No, JS doesn’t have pointers.
Objects are passed around by passing a copy of a reference. The programmer cannot access any C-like “value” representing the address of the object.
Within a function one may change the contents of an passed object via that reference, but you cannot modify the reference that the caller had, because your reference is only a copy:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
var foo = {'bar': 1}; //1 function tryToMungeReference(obj) //3 { obj = {'bar': 2}; // 4 } function mungeContents(obj) // 7 { obj.bar = 2; // 8 } tryToMungeReference(foo); // 2 foo.bar === 1; // true, 5 mungeContents(foo); // 6 foo.bar === 2; // true, 9 |