I'm trying to bring some sanity to a legacy Classic ASP application, and as part of this I'm trying to write a Fluent API for some JScript classes that I have created.
e.g. myClass().doSomething().doSomethingElse()
The concept is outlined here (in VBScript)
This is my sample JScript class:
var myClass = function () {
this.value = '';
}
myClass.prototype = function () {
var doSomething = function (a) {
this.value += a;
return this;
},
doSomethingElse = function (a, b) {
this.value += (a + b);
return this;
},
print = function () {
Response.Write('Result is: ' + this.value + "<br/>");
}
return {
doSomething: doSomething,
doSomethingElse: doSomethingElse,
print: print
};
}();
/// Wrapper for VBScript consumption
function MyClass() {
return new myClass();
}
In the existing VBScript code I'm then trying to chain the methods together:
dim o : set o = MyClass()
'' This works
o.doSomething("a")
'' This doesn't work
o.doSomething("b").doSomethingElse("c", "d")
'' This for some reason works
o.doSomething("e").doSomethingElse("f", "g").print()
When the functions have more than one parameter I get the "Cannot use parentheses when calling a Sub
" VBScript error. Strangely, it seems to work when followed by another method.
I understand that parentheses should be ommitted when calling a sub. However:
1. Why is it being recognised as a Sub if there is a return value?
2. Is there any way around this in order to implement my Fluent API?