One way to reduce memory bloat and initialization time is to declare
the local properties of an object as prototype properties. Prototype
properties do NOT take up extra space when an object is instantiated
UNLESS you change the instances' local value. For instance, 

function myObj(){
  this.fooForLuv = 2;
};

takes up more memory/time than

function myObj(){
};
myObj.prototype.foForLuv = 2;

A longer example is my rewrite of the button widget. Note that I
share the methods for mouse events from the prototype, instead of
declaring them for each instance, as well as setting the button
default colors as class prototype properties. ( I also retain a
reference to component layers to allow for later color changes on
rollover and such.)

// CODE ///////////

function dButton() {   // Image() or "string"
  this.onConstruct(this, arguments);
};
dButton.prototype = new DynLayer;

// FEATURES

dButton.prototype.buttonColor = '#CECECE';

...

dButton.prototype.hiliteColor     = '#FFFFFF';
...

// METHODS

dButton.prototype.onConstruct = function(db, args){
    db.DynLayer = DynLayer;
    db.DynLayer();
  
    if (typeof(args[0])=="string") {
      db.setText(args[0]);
    } else if (typeof(args[0])=="object") {
      db.setImage(args[0]);
    }
  
    db.face   = new DynLayer(null,0,0,1,1,db.buttonColor, true, 3);
    db.addChild(db.face);
  
    db.hilite = new DynLayer(null,0,0,1,1,db.hiliteColor, true, 1);
    db.addChild(db.hilite);
  
    db.shadow = new DynLayer(null,0,0,1,1,db.buttonColor, true, 2);
    db.addChild(db.shadow);
  
    db.setBgColor(args[1] || db.buttonColor);
  
    db.listener = new EventListener(db);
    db.listener.oncreate    = dButton.prototype.liOnCreate;
    db.listener.onresize    = dButton.prototype.liOnResize;
    db.listener.onmousedown = dButton.prototype.liOnMousedown;
    db.listener.onmouseup   = dButton.prototype.liOnMouseup;
    db.listener.onmousemove = dButton.prototype.liOnMousemove;
    db.listener.onmouseout  = dButton.prototype.liOnMouseout;
    db.addEventListener(db.listener);
};

...

// listener methods

dButton.prototype.liOnCreate = function(e) {
  var o = e.getTarget();
  o.setButtonColor(this.buttonColor);
  if (o.label) o.setText(o.text)
  if (o.w==null && o.h==null) {
    if (o.label) o.setSize(o.label.w+4,o.label.h+4);
    else if (o.imglyr) o.setSize(o.imglyr.w+4,o.imglyr.h+4);
  } else o.recenter();
  if (o.label) {
    o.label.setVisible(true);
  } else if (o.imglyr) o.imglyr.setVisible(true);
};

...

dButton.prototype.liOnMouseup = function(e) {
  var o = e.getTarget();
  if (o.enabled) o.setUp();
  if (this.dataItem) this.dataItem.onClick(o);
};

///// END CODE //////////////

Also I always use a discrete onConstruct() method so that if I decide
to subclass a widget I can avail myself of the parent classes'
constructor. 

For instance if I decide to write a child class of dButton(even if
the only reason is to have a different default color scheme), it
would look like this:

// CODE ////////

function dBlackButton(){
  this.onConstruct(this, arguments);
};

dBlackButton.prototype = new dButton();
dBlackButton.prototype.buttonColor = '#000000';
dBlackButton.prototype.hiliteColor = '#999999';
dBlackButton.prototype.labelColor  = '#FFFFFF';

// END CODE ////////

that's a pretty tight package for a whole new subclass! Also if you
need to work code around the construction sequence you can write a
new onConstruct() method that takes advantage of the old code:

/// CODE ////////

dBlackButton.parentClass = new dButton();

dBlackButton.prototype.onConstruct(dbb, args){
  this.parentClass.onConstruct(dbb, args);
  dbb.foo = 1;
  dbb.bar();
};

=====
=======================
'Providing year 2001 consultations at rates as low as $5,000 an hour.'
Dave Edelhart
Director of Operations, Manatee Bay Productions
www.manateebay.com    
[EMAIL PROTECTED]

__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/

_______________________________________________
Dynapi-Help mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/dynapi-help

Reply via email to