It looks like you really want to associate Man instances with a Person instance that saves any properties added in its constructor, in which case you might really need something like this:
function Person(a, b) { this.a = a; this.b = b; } function Man() {} Man.prototype = new Person("val1", "val2"); var m = new Man(); console.log(ma);
... which prints val1 .
Additional Ramblings
The purpose of inherit is to create an object from a function whose prototype is the given superclass (without explicitly using new ), which is what it does. Therefore, the following fingerprints are string :
function Person() {} Person.prototype.test = "string"; function Man() {} function inherit(superClass) { function F() {} F.prototype = superClass.prototype; return new F; } var t = inherit(Person); console.log(t.test);
But you usually want to assign the returned object to another function prototype:
Man.prototype = inherit(Person); var m = new Man(); console.log(m.test);
... so m.test also prints string , which means that the objects created with Man are associated with Person prototype ).
Note that Man.prototype.prototype undefined and - this is an important part - is also pointless. Functions have prototypes. There are no other objects (for example, Man.prototype ). The prototype property is not magic in any other context. Assigning a value to a prototype object does nothing special. This is just another property.
Note also that the thing we returned from inherit is related to Person through its prototype and does not have access to any properties added to Person instances.
source share