From ddjolley at gmail.com Mon Feb 25 15:13:35 2008 From: ddjolley at gmail.com (Doug Jolley) Date: Mon, 25 Feb 2008 15:13:35 -0800 Subject: [OCLUG-devel] Javascript - Overriding method definition Message-ID: 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 From msimpson at braysimpson.com Mon Feb 25 18:33:10 2008 From: msimpson at braysimpson.com (Morgan Simpson) Date: Mon, 25 Feb 2008 18:33:10 -0800 Subject: [OCLUG-devel] Javascript - Overriding method definition In-Reply-To: References: Message-ID: <47C37A66.5040901@braysimpson.com> Doug Jolley wrote: > 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? Commercial systems typically solve this problem with the addition of a discount policy attribute. Your "amount" function would examine this attribute to determine which policy (of potentially many) applies to this transaction. -- Best regards, Morgan Simpson Bray, Simpson & Associates, Inc. +1 714 435 0150 - office +1 714 390 5040 - cell +1 714 549 3064 - fax