60

クラスBがクラスAからすべてを継承する(クラスBがAを拡張する)ようにJavaScriptクラスを拡張する正しい/最良の方法は何ですか?

4

3 に答える 3

57

Take a look at Simple JavaScript Inheritance and Inheritance Patterns in JavaScript.

The simplest method is probably functional inheritance but there are pros and cons.

于 2010-02-13T00:57:56.400 に答える
30

Douglas Crockford has some very good explanations of inheritance in 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
于 2010-02-13T01:09:53.457 に答える
1
   extend = function(destination, source) {   
          for (var property in source) {
            destination[property] = source[property];
          }
          return destination;
    };

JavaScript の拡張

for ループにフィルターを追加することもできます。

于 2012-10-28T15:28:31.170 に答える