The problem is that you can't easily create a prototype object for B
since invoking the constructor of A
can't be done. This is due to the parameters for the constructor being unknown before new B
is executed. You need a dummy constructor function to construct a prototype for B
that links to A
's prototype.
B.prototype = (function(parent){
function protoCreator(){};
protoCreator.prototype = parent.prototype;
// Construct an object linking to A.prototype without calling constructor of A
return new protoCreator();
})(A);
Once you've got the prototype object for B
set up, you need to ensure to call the constructor of A
in the constructor of B
.
function B(x, y) {
// Replace arguments by an array with A's arguments in case A and B differ in parameters
A.apply(this, arguments);
}
You should now be able to instantiate B
by calling new B(x, y)
.
For a complete same including parameter validation in A
see a jsFiddle.
In your original code you are setting B.prototype.constructor = B
. I'm not getting why you are doing this. The constructor
property does not influence the inheritance hierarchy for which the prototype
property is responsible. If you want to have the named constructor contained in the constructor
property you'd need to extend the code from above a little:
// Create child's prototype – Without calling A
B.prototype = (function(parent, child){
function protoCreator(){
this.constructor = child.prototype.constructor
};
protoCreator.prototype = parent.prototype;
return new protoCreator();
})(A, B);
Using the first definition of B.prototype
you'd get the following results:
var b = new B(4, 6);
b.constructor // A
console.info(b instanceof A); // true
console.info(b instanceof B); // true
With the extended version, you'll get:
var b = new B(4, 6);
b.constructor // B
console.info(b instanceof A); // true
console.info(b instanceof B); // true
The cause for the different output is that instanceof
follows up the whole prototype chain of b
and tries to find a matching prototype object for A.prototype
or B.prototype
(in the other call). The b.constructor
prototype does refers to the function that was used to define the instances prototype. In case you wonder why it does not point to protoCreator
this is because its prototype was overwritten with A.prototype
during the creation of B.prototype
. The extended definition as show in the updated example fixes this constructor
property to point to a more appropriate (because probably more expected) function.
For daily use, I'd recommend to discard the idea of using the constructor
property of instances entirely. Instead do use instanceof
since its results are more predictable/expected.