Consider the following example:
class Car {
constructor(name) {
this.kind = 'Car';
this.name = name;
}
printName() {
console.log('this.name');
}
}
My goal is to define printName using a different approach, like this:
class Car {
constructor(name) {
this.kind = 'Car';
this.name = name;
}
// we aim to redefine printName with a different scope
// the syntax is close, but not exactly correct
printName: makePrintName(foo, bar, baz)
}
where makePrintName is a functor, similar to this:
exports.makePrintName = function(foo, bar, baz){
return function(){ ... }
};
Is this achievable with ES6? My editor and TypeScript are not approving of this
NOTE: Achieving this with ES5 was simple and looked like this:
var Car = function(){...};
Car.prototype.printName = makePrintName(foo, bar, baz);
Currently, the best solution using class syntax for me is:
const printName = makePrintName(foo, bar, baz);
class Car {
constructor(){...}
printName(){
return printName.apply(this, arguments);
}
}
However, this solution is not ideal. The limitations of using class syntax become evident when trying to achieve what was easily done with ES5 syntax. The ES6 class wrapper is, therefore, a leaky abstraction.
For a real-life use case, please refer to:
https://github.com/sumanjs/suman/blob/master/lib/test-suite-helpers/make-test-suite.ts#L171
The issue with using
TestBlock.prototype.startSuite = ...
is that in that scenario, I cannot simply return the class on line:
https://github.com/sumanjs/suman/blob/master/lib/test-suite-helpers/make-test-suite.ts#L67