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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, it will not automatically update itself. The reason is that what you are doing is assigning the value of the
lengthproperty, which is a number, to the variablen. Hence,nis 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 doingvar o = document.getElementById('A');would work in the manner you describe – what you’re assigning toois an object and not a primitive type.Note: By “primitive type” I mean any of the following: Undefined, Null, Boolean, Number, or String