You cannot, there is only one prototype chain. You basically have three options:
Inherit from one, copy another
, , . , , , , .
:
var ab = {
aness: new A(),
bness: new B()
};
ab.aness.a === 'a'; // true
ab.bness.b === 'b'; // true
A.prototype.a = 'm';
ab.aness.a === 'm'; // true
B.prototype.b = 'n';
ab.bness.b === 'n'; // true
, ab A ( "A-ness" ) aness B ( "B-ness" ) bness.
, , " ", ( , ). , , .
A-ness B-ness of ab, , . , , , , A foo, - foo B. :
function AB() {
var aness, bness;
this.aness = aness = new A();
this.bness = bness = new B();
aness.foo = function() {
if (bness.b === 42) {
return "The answer";
}
return A.prototype.foo.call(this);
};
}
var ab = new AB();
, , ab. , .
, .
B A
A B, B A, ab B:
A = function () {
this.x = 'x';
};
A.prototype.a = 'a';
B = function () {
this.y = 'y';
};
B.prototype = new A();
B.prototype.b = 'b';
ab = new B();
ab.a === 'a';
ab.b === 'b';
A.prototype.a = 'm';
ab.a === 'm';
B.prototype.b = 'n';
ab.b === 'n';
... , B A, , .
: var , , (, , ).
2. , , (, A B, ).