[OCLUG-devel] Javascript - Overriding method definition

Doug Jolley ddjolley at gmail.com
Mon Feb 25 15:13:35 PST 2008


I have the following:

function purchase(description,price) {
this.description=description;
this.price=price;
this.quantity=0;
this.buttonStatus="add";
this.amount=amount;
}
function amount() {
   return parseFloat((this.quantity * this.price)+'');
}

I then use a loop to populate an array with various purchase objects.
Typically, there would be some number like say 20 different purchase
objects contained in such an array.  As you can see, the amount of
each purchase is simply quantity*price.  However, for sake of example,
let's say that the 5th purchase object has a graduated pricing
structure where it's so much for the first n units, some lesser amount
for units n+1 through m, and an even smaller amount for units m+1 and
more.  Consequently, what I'd like to do is, after the array of
purchase objects has be set up, redefine the amount() method for only
the 5th purchase object to incorporate a formula that takes into
account the graduated pricing structure.  IOW, I'd like to override
the standard amount method definition for only the 5th object with a
new definition.  My question is:  How would I do that?

Thanks for any input.

         ... doug


More information about the OCLUG-devel mailing list