尝试使用 ES6 并尝试创建一个具有静态属性和函数的类来进行解析。然后我想为我正在解析的每种不同类型扩展基本解析器。不确定我是否在做反模式,但我不能覆盖静态属性。
这是我的基本解析器
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`;
}
}
然后我想像这样扩展
class ExtendedModule extends Module {
static name = 'Extended';
static version = {major: 1, minor:0}
static internalParser(data) {
//Some stuff
}
}
但是当我用 babel 在 node 中编译时,我得到了
true; if ('value' in descriptor) descriptor.writable = true; Object.defineProp
^
TypeError: Cannot redefine property: name
at Function.defineProperty (native)
有人知道这是可能的还是完全错误的?
最佳答案
您可以尝试使用静态 getter 在代码中实现最初预期的层次结构:
class Module {
static get name() { return "Default Module"; }
static get version() { return {major:10000, minor: 10000}; }
static parse() {
console.log( this.name );
}
}
class ExtendedModule extends Module {
static get name() { return "Extended"; }
static get version() { return {major:1, minor: 0}; }
}
ExtendedModule.parse();
使用 BabelJS 这变成了
"use strict";
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Module = function () {
function Module() {
_classCallCheck(this, Module);
}
_createClass(Module, null, [{
key: "parse",
value: function parse() {
console.log(this.name);
}
}, {
key: "name",
get: function get() {
return "Default Module";
}
}, {
key: "version",
get: function get() {
return { major: 10000, minor: 10000 };
}
}]);
return Module;
}();
var ExtendedModule = function (_Module) {
_inherits(ExtendedModule, _Module);
function ExtendedModule() {
_classCallCheck(this, ExtendedModule);
return _possibleConstructorReturn(this, (ExtendedModule.__proto__ || Object.getPrototypeOf(ExtendedModule)).apply(this, arguments));
}
_createClass(ExtendedModule, null, [{
key: "name",
get: function get() {
return "Extended";
}
}, {
key: "version",
get: function get() {
return { major: 1, minor: 0 };
}
}]);
return ExtendedModule;
}(Module);
ExtendedModule.parse();
正在显示的运行代码
Extended
在 JS 控制台上。
- ES5 fiddle :https://jsfiddle.net/dwq698r8/
- ES6 fiddle :https://jsfiddle.net/yd8bf7am/
关于Javascript es6 覆盖静态属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33103053/