在Javascript中继承静态属性
我在当前项目中使用的设计模式是:在Javascript中继承静态属性,javascript,class,inheritance,properties,static,Javascript,Class,Inheritance,Properties,Static,我在当前项目中使用的设计模式是: var MyConstructor = function() { ... }; MyConstructor.STATIC_PROPERTY = 'static'; 现在,如果我想从MyConstructor类继承,我会: var ChildClass = function() { ... }; ChildClass.prototype = new MyConstructor(); // Or Object.create(...) ChildClass.prot
var MyConstructor = function() { ... };
MyConstructor.STATIC_PROPERTY = 'static';
现在,如果我想从MyConstructor类继承,我会:
var ChildClass = function() { ... };
ChildClass.prototype = new MyConstructor(); // Or Object.create(...)
ChildClass.prototype.constructor = ChildClass;
问题是ChildClass。静态_属性未定义/未继承
有办法解决这个问题吗
第二个问题:
如果我console.log(MyConstructor)
,我会得到function(){…}
,当MyConstructor.STATIC属性实际存在时,我不会得到任何东西。最后存储的STATIC\u属性在哪里?如何检查/显示它?使用ES6类,该类通常从父类(构造函数函数对象)继承:
函数MyConstructor(){/*…*/}
MyConstructor.STATIC_属性='STATIC';
类ChildClass扩展MyConstructor{/*…*/}
log(ChildClass.STATIC_属性)代码>“静态”属性只是构造函数对象上的属性。它们与JavaScript继承方案没有任何关系。请尝试console.dir(MyConstructor)
查看静态属性。因为您可以在定义ChildClass
的范围内访问MyConstructor(),类似于ChildClass.STATIC\u PROPERTY=MyConstructor.STATIC\u PROPERTY
的东西应该可以工作。(但可能不可取)如前所述,它与原型遗传无关。它是静态的,不使用对象。defineProperty()
或它的替代项。我们可以随心所欲地覆盖它。可能重复“或对象。创建(…)”-