Trying out ES6 and tried to create a class with static properties and function for parsing. Then I want to extend the base parser for each different type I am parsing. Not sure if I am doing a anti-pattern but I cannot override static properties.
This is my base parser
class Module {
static name = 'Default Module'
static version = {major:10000, minor: 10000}
static checkVersion({majorVersion = 10000, minorVersion = 10000}) {
if(this.version.major !== majorVersion || this.version.minor > minorVersion) {
throw `${this.name} requires version ${this.version.major}.${this.version.minor} got ${majorVersion}.${minorVersion}`;
}
}
static parse(data) {
try {
this.checkVersion(data);
return this.internalParser(data);
} catch (e) {
throw e;
}
}
static internalParser(data) {
throw `${this.name} has no parser implemented`;
}
}
And then I want to extend like this
class ExtendedModule extends Module {
static name = 'Extended';
static version = {major: 1, minor:0}
static internalParser(data) {
//Some stuff
}
}
But when compiling in node with babel I get
true; if ('value' in descriptor) descriptor.writable = true; Object.defineProp
^
TypeError: Cannot redefine property: name
at Function.defineProperty (native)
Anyone got a clue if this is even possible or just plain wrong?
Module.name
is"Module"
(it's still a named constructor function). You cannot put another.name
on it. – Hove.name
on it. That's great about Javascript: you can break things that easily, but you still can do. ;) – NortherlyModule
. This property isn't writable, right, but configurable. And thus it is still possible to "put another.name
on it". – Northerly