2

I'm new to JavaScript and trying to understand the memory management related to objects using this Mozilla reference: MDN Memory Management.

I am following an example, but having issues in understanding the references.

var o = { a: { b:2 } }; // 2 objects are created. One is referenced by the other as one of its property. // The other is referenced by virtue of being assigned to the 'o' variable. // Obviously, none can be garbage-collected var o2 = o; // the 'o2' variable is the second thing that // has a reference to the object o = 1; // now, the object that was originally in 'o' has a unique reference // embodied by the 'o2' variable var oa = o2.a; // reference to 'a' property of the object. // This object has now 2 references: one as a property, // the other as the 'oa' variable o2 = "yo"; // The object that was originally in 'o' has now zero // references to it. It can be garbage-collected. // However what was its 'a' property is still referenced by // the 'oa' variable, so it cannot be free'd oa = null; // what was the 'a' property of the object originally in o // has zero references to it. It can be garbage collected. 

I got confused by terms like this object, one is referenced by the other, 2 objects are created - for what? 'o' & 'a'?, that has a reference to the object - which object?

Can someone rephrase the comments above with the actual object names, please?

It may consider as a spoonfeeding question but let me know if it's not worth to ask this question. I'll delete it.

2
  • Objects don't have names; that's kind of the point. There are either variables (or object properties) referencing an object or there aren't. Commented Sep 4, 2014 at 20:19
  • JavaScript has literal notation for objects. These objects can have other objects as their properties. Commented Sep 4, 2014 at 20:20

1 Answer 1

1

It is a bit of a crummy explanation. I'll give you a rough edition.

var o = { a: { b:2 } }; // 2 objects are created. One (the value of the property named "a") // is referenced by the other (the value of the variable named "o") // as its property. // The other (the value of the variable named "o") // is referenced by virtue of being assigned to the 'o' variable. // Obviously (maybe to the author...), none can be garbage-collected var o2 = o; // the 'o2' variable now also // has a reference to the object (the value of the variable "o") o = 1; // "o" now refers to something else and "o2" is the only variable // referring to the original "o" object. var oa = o2.a; // reference to the 'a' property of the "o2" object. o2 = "yo"; // The object that was originally in 'o' has now zero // references to it, but // the object's 'a' property is still referenced by // the 'oa' variable, so the "o2" object cannot yet // be GC'ed. oa = null; // The object now has zero references to it, so it can be // garbage collected. 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.