What is the right/best way to extend a javascript class so Class B inherits everything from the class A (class B extends A)?
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.
Annabelle
Douglas Crockford has some very good explanations of inheritance in 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
extend = function(destination, source) {
for (var property in source) {
destination[property] = source[property];
}
return destination;
};
You could also add filters into the for loop.
来源:https://stackoverflow.com/questions/2256040/javascript-extends-class