I was wondering if there is the possibility to create methods, with the same body, in TypeScript. In JavaScript, I can extend the class with 'Prototype':
var methods = [
{
'name' : 'f_1',
'argumentsLength' : 1
},
{
'name' : 'f_2',
'argumentsLength' : 2
}
];
var Person = function (name) {
this.name = name;
};
for (var k = 0; k < methods.length; k++) {
!function (fName, fParamsLength) {
Person.prototype[fName] = function () {
if (arguments.length !== fParamsLength) {
throw new Error ('Wrong number of parameters.');
}
console.log ('Function name : ' + fName);
for (var k = 0; k < arguments.length; k++) {
console.log ('arguments[' + k + '] : ' + arguments[k]);
}
console.log ();
};
} (methods[k].name, methods[k].argumentsLength);
}
var p = new Person ('Jumbo');
p.f_1 (1);
p.f_2 (2, 3);
I cannot do the same in TypeScript. I have an error when I try to call the methods 'f_1' and 'f_2' since I didn't declare them explicitly.