Javascript prototype chain

Suppose I have two constructor functions:

var Person = function(xx,xx,xxx,xxxxxxx) { //Person initialization } var Man = function(xx,xx,xxx,xxx) { //Man initialization } 

And I want the Man to extend from the Person.

Here is what I thought:

Given the created Man object:

 var m=new Man("xx",....); 

1) when accessing property m he will look for it in Man.prototype .

2) If not found, he must find it in Man.prototype.__prop__ .

So what I did is make Man.prototype.__prop__ related to Person.prototype .

I know this is the usual way:

 function inherit(superClass) { function F() {} F.prototype = superClass.prototype; return new F; } Man.prototype=inherit(Person); 

But when I try this:

 Man.prototype.prototype=Person.prototype. 

Why is this not working?

+4
source share
3 answers

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.

+6
source

How do I do this (regarding your example):

 var Person = function () { } // Define "Person" methods var Man = function () { } Man.prototype = new Person(); // Define "Man" methods 

UPDATE

Regarding constructors with parameters: just found this SO question that can really help you figure it out (second answer, first part): JavaScript Inheritance: when the constructor has arguments .

+3
source

There are many common methods for this. I will provide three of them:

1.) Using the Function object as a constructor and as a prototype object for inherited new objects. The implementation should look like this:

 var person = { toString : function() { return this.firstName + ' ' + this.lastName; } } function extend(constructor, obj) { var newObj = Object.create(constructor); for (var prop in obj) { if (obj.hasOwnProperty(prop)) { newObj[prop] = obj[prop]; } } return newObj; } var man = extend(person, { sex: "male", age: "22" }); var name = extend(man, { firstName: "Simo", lastName: "Endre" }); name.man; name.toString(); 

2.) In this case, we will use the Function object as a constructor for modeling classical inheritance in C # or Java. The prototype property of the object will be used as the constructor, and the newly created object will inherit all the properties from the root of the object's prototype. In this case, the prototype of the object has only one type of increase function, an effective implementation is performed in the function method.

 var Person = function(firstName, lastName) { this.firstName = firstName; this.lastName = lastName; } Person.prototype.toString = function() { return this.firstName + ' ' + this.lastName; } function inherit(func) { // passing function arguments into an array except the first one which is the function object itself var args = Array.prototype.slice.call(arguments, 1); // invoke the constructor passing the new Object and the rest of the arguments var obj = Object.create(func.prototype); func.apply(obj, args); //return the new object return obj; } var man = inherit(Person, "Simo", "Endre"); man.toString(); 

3.) Well-known inheritance model:

 function extend(o) { function F() {} // We'll set the newly created function prototype property to the object. //This act as a constructor. F.prototype = o; // Using the `new` operator we'll get a new object whose prototype is o. return new F(); }; 
0
source

All Articles