tags:

views:

24

answers:

2

For example if I assign

var n = document.getElementById('A').childNodes.length;

And then later append a child to A, would n update itself or would I have to assign it the new length again?

A: 

No, you will have to re evaluate the variable.

rahul
+5  A: 

No, it will not automatically update itself. The reason is that what you are doing is assigning the value of the length property, which is a number, to the variable n. Hence, n is not aware of the object property it came from, it merely stores a number. Primitive types in JavaScript are assigned/passed by value, whereas objects are passed by reference. This is why doing var o = document.getElementById('A'); would work in the manner you describe - what you're assigning to o is an object and not a primitive type.

Note: By "primitive type" I mean any of the following: Undefined, Null, Boolean, Number, or String

Jimmy Cuadra