Chaining methods in LotusScript
Category show-n-tell thursday
One characteristic now common to several popular Javascript libraries (such as Mootools, jQuery, and the newly updated Prototype) is the ability to chain methods: that is, to do several things to or with an object in a single line of code. For example:
Clown.makeBalloonAnimal().rideUnicycle().frightenSmallChildren();
This appeals to many of us because, well, we're lazy. That's not a bad thing - the point of code is to make some task or process easier for users; so why not structure code in a way that makes it easier to reference that code later on?
Anyhow, what makes this possible in Javascript is that all methods are functions. Even what LotusScripters would consider a Sub. Which means that all methods have a return value, even if none is explicitly specified. A chainable method, then, is simply a function that returns an object (typically the instance containing the method).
In LotusScript, this is easily accomplished by making each Sub a Function instead and using the keyword Me:
All methods of the class are now chainable:
One characteristic now common to several popular Javascript libraries (such as Mootools, jQuery, and the newly updated Prototype) is the ability to chain methods: that is, to do several things to or with an object in a single line of code. For example:
Clown.makeBalloonAnimal().rideUnicycle().frightenSmallChildren();
This appeals to many of us because, well, we're lazy. That's not a bad thing - the point of code is to make some task or process easier for users; so why not structure code in a way that makes it easier to reference that code later on?
Anyhow, what makes this possible in Javascript is that all methods are functions. Even what LotusScripters would consider a Sub. Which means that all methods have a return value, even if none is explicitly specified. A chainable method, then, is simply a function that returns an object (typically the instance containing the method).
In LotusScript, this is easily accomplished by making each Sub a Function instead and using the keyword Me:
Public Class Clown Public Function makeBalloonAnimal() As Clown 'Method statements Set makeBalloonAnimal = Me End Function Public Function rideUnicycle() As Clown 'Method statements Set rideUnicycle = Me End Function Public Function frightenSmallChildren() As Clown 'Method statements Set frightenSmallChildren = Me End Function End Class
All methods of the class are now chainable:
Sub Initialize Dim Bozo As Clown Set Bozo = New Clown() Call Bozo.makeBalloonAnimal().rideUnicycle().frightenSmallChildren() End Sub

Comments
Chapeau bas! Tim
Posted by Alain H Romedenne At 02:47:42 PM On 02/09/2007 | - Website - |
Posted by Nathan T. Freeman At 06:38:10 PM On 01/27/2007 | - Website - |