Javascript 扩展类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2256040/
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
Javascript extends class
提问by xpepermint
What is the right/best way to extend a javascript class so Class B inherits everything from the class A (class B extends A)?
扩展 javascript 类以便 B 类继承 A 类的所有内容(B 类扩展 A)的正确/最佳方法是什么?
采纳答案by cletus
Take a look at Simple JavaScript Inheritanceand Inheritance Patterns in JavaScript.
查看JavaScript中的简单 JavaScript 继承和继承模式。
The simplest method is probably functional inheritance but there are pros and cons.
最简单的方法可能是函数继承,但有利有弊。
回答by Annabelle
Douglas Crockford has some very good explanations of inheritance in JavaScript:
Douglas Crockford 对 JavaScript 中的继承有一些很好的解释:
- prototypal inheritance: the 'natural' way to do things in JavaScript
- classical inheritance: closer to what you find in most OO languages, but kind of runs against the grain of JavaScript
回答by Annabelle
extend = function(destination, source) {
for (var property in source) {
destination[property] = source[property];
}
return destination;
};
You could also add filters into the for loop.
您还可以将过滤器添加到 for 循环中。

