[Helma-user] __defineGetter__

Michael Platzer michael.platzer at knallgrau.at
Fri Mar 30 14:58:42 CEST 2007


Hi list,

One of the cool new features of Helma 1.6.0 will be the ability to 
define methods for retrieving and setting properties throught the use of 
defineGetter, and defineSetter.
-> 
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Creating_New_Objects:Defining_Getters_and_Setters

With every new feature the question arises, how to leverage its power 
wisely in your own helma app. One of the ideas that just struck me, was 
to define properties 'first' and 'last' for every collection.

  HopObject.prototype.__defineGetter__("first", function() { return 
this.get(0); });
  HopObject.prototype.__defineSetter__("first", function() { return; });
  HopObject.prototype.__defineGetter__("last", function() { return 
this.get(this.count() - 1); });
  HopObject.prototype.__defineSetter__("last", function() { return; });

This has the nice effect, that i  can do the following with deep macro 
invocations:

  <% this.comments.first.title | truncate max="100" %>

Another idea is to use __defineGetter__ to create objects on the fly, 
when they are needed:
  SomeObject.prototype.__defineGetter__("tags", function() {
     if (this.tagMgr == null) {
        var tagMgr = new TagMgr();
        tagMgr.target = this;
        this.tagMgr = tagMgr;
     }
     return this.tagMgr;
  });

Or would you consider this to be hackish programming practice? What 
other usage scenarios come to your mind, when thinking of using 
__defineGetter__?

would love to hear some different thoughts on this,

  michi



More information about the Helma-user mailing list