如何从 JavaScript 中的基类继承静态方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5441508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to inherit static methods from base class in JavaScript?
提问by William Choi
I'm trying to achieve some basic OOP in JavaScript with the prototype way of inheritance. However, I find no way to inherit static members (methods) from the base class.
我正在尝试使用继承的原型方式在 JavaScript 中实现一些基本的 OOP。但是,我发现无法从基类继承静态成员(方法)。
We can simulate the basic class model by using prototype:
我们可以使用prototype来模拟基本类模型:
SomeClass = function(){
var private_members;
this.public_method = function(){
//some instance stuff..
};
};
Class.static_method = function(){
//some static stuff;
};
//Inheritance
SubClass = function(){ //sub-class definition };
SubClass.prototype = new Class();
However, SubClass
doesn't inherit static_method
from Class
.
但是,SubClass
不继承static_method
自Class
.
采纳答案by donavon
Try this:
试试这个:
class BaseClass {
static baseMethod () {
console.log("Hello from baseMethod");
}
}
class MyClass extends BaseClass {
constructor(props) {
super(props);
}
}
Object.assign(MyClass, BaseClass);
They key is Object.assign
which should be everyone's new best friend. You can now call any base method from BaseClass
using MyClass
as follows:
他们的关键是Object.assign
哪个应该是每个人的新好朋友。您现在可以BaseClass
使用MyClass
以下方法调用任何基本方法:
MyClass.baseMethod();
You can see this live and in action on this pen.
您可以在这支笔上实时查看这些内容。
Enjoy!
享受!
回答by DragonDTG
In the classical (OO) inheritance pattern, the static methods do not actually get inherited down. Therefore if you have a static method, why not just call: SuperClass.static_method()
whenever you need it, no need for JavaScript to keep extra references or copies of the same method.
在经典 (OO) 继承模式中,静态方法实际上并没有被继承下来。因此,如果你有一个静态方法,为什么不直接调用:SuperClass.static_method()
只要你需要它,JavaScript 就不需要保留额外的引用或相同方法的副本。
You can also read this JavaScript Override Patternsto get a better understanding of how to implement inheritance in JavaScript.
您还可以阅读此JavaScript Override Patterns以更好地了解如何在 JavaScript 中实现继承。
回答by Jeffery To
After your example code you can do this:
在您的示例代码之后,您可以执行以下操作:
for (var i in Class) {
SomeClass[i] = Class[i];
}
to copy static members from Class
to SubClass
.
将静态成员从 复制Class
到SubClass
。
If you're using jQuery, have a look at the jQuery.Classplugin from JavaScriptMVC. Or you can extend John Resig's Simple JavaScript Inheritancefor a more library-agnostic system.
如果您使用 jQuery,请查看JavaScriptMVC中的jQuery.Class插件。或者,您可以扩展 John Resig 的Simple JavaScript Inheritance以获得与库无关的系统。
回答by Amr Labib
For ES5 you will need to use Object.assign
to copy static methods from BaseClass to SubClass but for ES6 it should work without using Object.assign
对于 ES5,您需要使用Object.assign
将静态方法从 BaseClass 复制到 SubClass,但对于 ES6,它应该可以在不使用的情况下工作Object.assign
ES5 Example
ES5 示例
var BaseClass = function(){
}
BaseClass.sayHi = function(){
console.log("Hi!");
}
var SubClass = function(){
}
Object.assign(SubClass , BaseClass);
BaseClass.sayHi(); //Hi
SubClass.sayHi(); //Hi
ES6 Example
ES6 示例
class BaseClass {
static sayHi(){
console.log("Hi!");
}
}
class SubClass extends BaseClass{
}
BaseClass.sayHi() //Hi
SubClass.sayHi() //Hi
回答by bobince
How are you implementing 'static' methods? Since JavaScript doesn't have a native class/instance object model, it all depends on how you've designed your own class system.
您如何实现“静态”方法?由于 JavaScript 没有本机类/实例对象模型,这完全取决于您如何设计自己的类系统。
If you want to be able to inherit anything it'll have to be using the SomeClass.prototype
object, rather than putting anything directly on the SomeClass
constructor function. So essentially you'll be defining static methods as normal instance methods, but ones that don't care about what value of this
is passed into them.
如果您希望能够继承任何东西,就必须使用该SomeClass.prototype
对象,而不是将任何东西直接放在SomeClass
构造函数上。所以本质上,您将静态方法定义为普通的实例方法,但不关心this
传递给它们的值是什么。
回答by Hitmands
you can access static fields
via this.constructor[staticField]
;
您可以static fields
通过访问this.constructor[staticField]
;
class A {
static get Foo() { return 'Foo'; }
constructor() {
console.log(`Class ${this.constructor.name}`, this.constructor.Foo);
}
}
class B extends A {
static get Foo() { return 'Baz'; }
}
class C extends A {}
const a = new A();
const b = new B();
const c = new C()