Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javascript中继承静态属性_Javascript_Class_Inheritance_Properties_Static - Fatal编程技术网

在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()
或它的替代项。我们可以随心所欲地覆盖它。可能重复“或对象。创建(…)”-