The problem is that you cannot easily create a prototype object for B , since the call to constructor A cannot be performed. This is because the parameters for the unknown constructor are executed before the execution of new B To create a prototype for B that references prototype A , you need a constructor-dummy constructor function.
B.prototype = (function(parent){ function protoCreator(){}; protoCreator.prototype = parent.prototype;
Once you have the prototype object for B installed, you need to provide a call to constructor A in constructor B
function B(x, y) {
Now you can create an instance of B by calling new B(x, y) .
To fully enable parameter checking in A see jsFiddle .
In the source code, you set B.prototype.constructor = B I do not understand why you are doing this. The constructor property does not affect the inheritance hierarchy for which the prototype property corresponds. If you want to have the named constructor contained in the constructor property, you need to extend the code a bit above:
// Create child 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 B.prototype definition, you will 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
In the advanced version you will receive:
var b = new B(4, 6); b.constructor // B console.info(b instanceof A); // true console.info(b instanceof B); // true
The reason for the other conclusion is that instanceof follows the entire B prototype chain and tries to find a suitable prototype object for A.prototype or B.prototype (in another call). The prototype b.constructor refers to the function that was used to determine the prototype of the instances. If you are wondering why it does not point to protoCreator , this is because its prototype was overwritten by A.prototype during the creation of B.prototype . The extended definition shown in the updated example corrects this constructor property to indicate a more appropriate (as, probably, more expected) function.
For daily use, I would recommend abandoning the idea of ββusing the entire constructor property of instances. Use instanceof instead, as its results are more predictable / expected.