4
I’ve seen a lot of ways to do it inheritance in javascript, but do not know how to do inheritance-multiple I will demonstrate an example of the problem:
function Transporte() {
var nome;
this.getNome = function () {
return nome;
};
this.setNome = function (value) {
nome = value;
};
}
function Motor() {
var motor;
this.getMotor = function () { return motor; };
this.setMotor = function (value) { motor = value; };
}
function Propulsor() {
var propulsor;
this.getTurbina = function () { return propulsor; };
this.setTurbina = function (value) { propulsor = value; };
}
Motor.prototype = new Transporte();
Propulsor.prototype = new Transporte();
function document_OnLoad() {
var carro = new Motor();
var aviao = new Propulsor();
carro.setMotor('4.1');
carro.setNome('opala');
aviao.setTurbina('123');
aviao.setNome('Teco-Teco');
console.log(carro.getMotor()+' '+carro.getNome());
console.log(aviao.getTurbina()+' '+aviao.getNome());
}
In this way that the source code was written, I have a simple inheritance:
- Car Motor Instance and Inherited Transport
- Aircraft Propulsion Instance and inherited from Transport
My question is:
Use the prototype to inherit Engine, Propeller and Transport?
It has to use some feature to inherit Motor, Propeller and Transport?
I suggest taking a look at the concept of mixins, because Javascript does not support multiple inheritance directly (i.e. a prototype chain forms a simple tree, in which each object has only a single prototype, can no longer have). P.S. More information on the operation of prototypes and inheritance javascript.
– mgibsonbr