MyObject = function(){

  var privateMethod = function(){ /* do stuff */ };

  return {
    publicProperty:5,
    publicMethod:function(){ /* do stuff */ };
  };
}(); // run it right away


Any downsides to this?

Jonathan Snook said on June 20, 2007

@Jack Turner: I like attaching it to the object as it's clear at the bottom of the function what I'm doing. If I have more than a couple functions then I have to scroll up to see where the object literal starts. And it more closely matched the second example, which again makes it clear to me what it is I'm attaching before returning the object. But this is purely preference.

Rubiwachs said on June 22, 2007

Jonathan, could you elaborate more about your calendar example in 'code'?
How do you mean:

"The great thing about this was that each of the calendars could basically talk to each other while still having access to some core common values like what the currently selected day, month and year was."

I'd like to see how you approached this.

Jonathan Snook said on June 22, 2007

@Rubiwachs: Unfortunately, it was for a client project, so I'm not allowed to publish what I did. The structure is much like that last example though where private objects are attached to a central object that gets returned. Internal calls use the internal API and external calls can use the external API.

Rubiwachs said on June 26, 2007

I was not looking for the code itself :), I am only interested in the structure of the code => 3 objects in the same namespace, accessing common values.
Would you do it like this:


if (typeof(MyNamespace) == 'undefined') var MyNamespace = {};

MyNamespace.MyObject1 = function() {
  var util = {
    _getValue: function() { return MyNamespace.value; },
    _setValue: function(value) { MyNamespace.value = value; }
  }

  var obj = {
    init: function() {},
    getValue: function() { return util._getValue(); },
    setValue: function(value) { util._setValue(value); }
  }

  obj.util = util;
  return obj;
}();

MyNamespace.MyObject2 = function() {
  var util = {
    _getValue: function() { return MyNamespace.value; },
    _setValue: function(value) { MyNamespace.value = value; }
  }

  var obj = {
    init: function() {},
    getValue: function() { return util._getValue(); },
    setValue: function(value) { util._setValue(value); }
  }

  obj.util = util;
  return obj;
}();

// set currentValue via MyObject1
MyNamespace.MyObject1.setValue('test1');

// retrieve currentValue via different objects
alert(MyNamespace.MyObject1.getValue());
alert(MyNamespace.MyObject2.getValue());

Both alert's will return 'test1'.
Any thoughts?

Jonathan Snook said on June 26, 2007

In the case of the calendar project, it was set up like this:

calendar = function(){
  var cal = { d:1, m:1, y:2006 }
  var monthView = {
     getEvents:function(){
         var url = '/getevents?d= ' + cal.d
                    + '&m=' + cal.m
                    + '&y=' + cal.y;
         Ajax.call(url);
     }
  }
  cal.month = monthView;
  return cal;
}();

This is more of a pseudo-code but you can see that the monthView object is still available externally via the calendar.month object and internally via the monthView object. Likewise, the monthView object can access the cal object and any of its properties. I had an object for both the day and weekly views as well, attached to the cal object in the same way.

DarrylHebbes said on August 29, 2007

How would one go about setting the 'publicProperty' in the sub-module pattern from another method in the object.

for example:

MyObject = function(){
  var util = {
    privateMethod: function(){ 

       obj.publicProperty = 10;  // <- Is this possible?
    };
  }

  var obj = {
    publicProperty:5,
    publicMethod:function(){ util.privateMethod(); };
  }

  // do my object attachment
  obj.util = util;
  return obj;
}(); // run it right away
Jonathan Snook said on August 29, 2007

@DarrylHebbes: you hit it on the nose. You can access methods of other objects within that scope. It makes a handy shortcut and is exactly how I usually approach things.

DarrylHebbes said on August 29, 2007

Thanks very much, been reading your posts since 2002...

rubudgirl said on January 16, 2009

DoD°DoD?Dμ D??€D°D·D′D?D?DoD? D2D°D? D??€D°D2?????????

holremasha said on January 17, 2009

D? DoD°Do D·D?D2???? D2D°??D??… D·D2Dμ?€????DμDo?
D£ D?DμD??? DoD??? D‘D°?€?? (D±D°D?D±Dμ?? Dμ?‰Dμ ??D???)
D?D???DoD° D??????‘D?D°

Sorry, comments are closed for this post. If you have any further questions or comments, feel free to send them to me directly.
f2富二代官网入口