I ran into a similar problem. As a result, I used a separate constructor for the internal object.
function engine(cc, fuel) { this.cc = cc; this.fuel = fuel } function car(type, model, cc, fuel) { this.type = type; this.model = model; this.engine = new engine(cc, fuel); } var mycar = new car("sedan", "toyota corolla", 1500, "gas"); console.log(mycar.type);
If I had any methods on prototypes of the 'engine' or 'car' constructors, they would still be available. However, I cannot take the car class out of the engine class in the sense of OOP. I didnโt have to. The "engine" is used as a component of the "car".
Meanwhile, for inheritance, I prefer to use the new methods included in ECMAScript 5, which means using Object.create, Object.defineProperties, etc. They are supported even in IE9. Before that, I used the same apply () method proposed by Costa.
source share