__defineGetter__

Michael Platzer <[email protected]>
Newsgroups gmane.comp.java.helma.general
Message-ID <[email protected]>
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.