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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:29:35  来源:igfitidea点击:

Javascript extends class

javascriptextends

提问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 中的继承有一些很好的解释:

  1. prototypal inheritance: the 'natural' way to do things in JavaScript
  2. classical inheritance: closer to what you find in most OO languages, but kind of runs against the grain of JavaScript
  1. 原型继承:在 JavaScript 中做事的“自然”方式
  2. 经典继承:更接近您在大多数面向对象语言中发现的内容,但有点违反 JavaScript 的规则

回答by Annabelle

   extend = function(destination, source) {   
          for (var property in source) {
            destination[property] = source[property];
          }
          return destination;
    };

Extending JavaScript

扩展 JavaScript

You could also add filters into the for loop.

您还可以将过滤器添加到 for 循环中。