Я бы сделал это с двумя различными функциями:
class X extends Y {
constructor(args) {
super(...baseArgs(args));
otherArgs(this, args);
}
}
function baseArgs(required_args) {
return [required_args.arg1, required_args.arg2, …];
}
function otherArgs(instance, args) {
// Find some way to separate the optional args and do something else with those...
}
Тем не менее, вы можете поместить вызов super()
в функцию стрелки и передать его в качестве обратного вызова:
class X extends Y {
constructor(args) {
applyArgs(args, (...baseArgs) => {
super(...baseArgs);
return this;
});
}
}
function applyArgs(args, init) {
const instance = init(args.arg1, args.arg2, …);
// Find some way to separate the optional args and do something else with those...
}
Но на самом деле, если несколько классов должны иметь одинаковое поведение, почему бы просто не дать им один и тот же суперкласс?
class Z extends Y {
constructor(args) {
super(args.arg1, args.arg2, …);
// Find some way to separate the optional args and do something else with those...
}
}
class X extends Z {}