If I were to define two objects myDataStore and myDrawer something like this:
var myDataStore = function(myObjectRef) {
this.myInternalObject = myObjectRef;
};
var myDrawer = function(myObjRef) {
this.myInternalObject = myObjectRef;
};
And if I were to create an object like so:
[[EDIT – Adjusted Object Creation to Ensure ‘this’ is being mapped to myObject, not the global window object]]
(function(){
var myObject = window.myObject = function(){
this.dataStore = new myDataStore(this);
this.drawer = new myDrawer(this);
}
})();
Then myObject.dataStore.myInternalObject, and myObject.drawer.myInternalObject, would simply be pointers back to the original ‘myObject’ – not taking up any additional memory in the browser. Yes?
I am interested in implementing techniques like this – as it makes it easy for objects to communicate with each other.
Yes, your assumption is correct.
myInternalObjectwill be a reference and not a new object. You can test it like this: