So, really, what you are here is a method in a class, for example:
class myClass {
constructor(private text: string) {
}
methodName() : string {
return this.text;
}
}
var a = new myClass('Example');
a.methodName();
If you just need a simple function, you can do this too:
function functor(text : string) : string {
return "abc";
}
functor('text');
The difference between JS-Style Functor and class output
You can try to play a functor, for example:
function functor(text : string) : () => string {
return function() {
return text;
}
}
var x = functor('Example');
x();
But actually using the class gives you very similar:
var myClass = (function () {
function myClass(text) {
this.text = text;
}
myClass.prototype.methodName = function () {
return this.text;
};
return myClass;
})();
var a = new myClass('Example');
a.methodName();
With the advantages of class semantics ... you can extend a class, you can create an interface from a class, etc.
source
share