Is this a safe way to reference objects in JavaScript?

Posted by John on Stack Overflow See other posts from Stack Overflow or by John
Published on 2010-06-01T15:03:40Z Indexed on 2010/06/01 15:13 UTC
Read the original article Hit count: 113

Filed under:

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:

(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.

© Stack Overflow or respective owner

Related posts about JavaScript