Need to understand what Parasitic Inheritance actually does.
I referred this link : Parasitic Inheritance in javascript
I need to understand what this example does :
Shape = {name: 'Shape'};
Shape.prototype.toString = function()
{
return this.name;
};
function Rectangle(width, height) {
var rect;
P = function() {};
P.prototype = Shape;
rect = new P();
rect.width = width;
rect.height = height;
return rect;
}
Second example works fine, but I need to know "Where does Parasitic inheritance e into picture ?"
var Person = function(name, age)
{
this.name = name;
this.age = age;
};
var Employee = function(name, age, group)
{
var e = new Person(name, age);
e.group = group;
return e;
};
var testname= new Employee('ABC', 30, 'Developer');
Thanks.
Need to understand what Parasitic Inheritance actually does.
I referred this link : Parasitic Inheritance in javascript
I need to understand what this example does :
Shape = {name: 'Shape'};
Shape.prototype.toString = function()
{
return this.name;
};
function Rectangle(width, height) {
var rect;
P = function() {};
P.prototype = Shape;
rect = new P();
rect.width = width;
rect.height = height;
return rect;
}
Second example works fine, but I need to know "Where does Parasitic inheritance e into picture ?"
var Person = function(name, age)
{
this.name = name;
this.age = age;
};
var Employee = function(name, age, group)
{
var e = new Person(name, age);
e.group = group;
return e;
};
var testname= new Employee('ABC', 30, 'Developer');
Thanks.
Share Improve this question edited Apr 23, 2014 at 9:07 Siddharth_Vyas asked Apr 23, 2014 at 8:48 Siddharth_VyasSiddharth_Vyas 10.1k10 gold badges42 silver badges69 bronze badges 6- But the link uses this example and I am not able to understand the logic. – Siddharth_Vyas Commented Apr 23, 2014 at 8:52
- Try to execute it somewhere. – thefourtheye Commented Apr 23, 2014 at 8:53
- @thefourtheye : Updated my question,second example works fine but need to understand the role of parasitic inheritance. – Siddharth_Vyas Commented Apr 23, 2014 at 9:08
-
P = function() {};
in the first example needs avar
- otherwise bees global. – kapa Commented Apr 23, 2014 at 9:12 -
Shape.prototype.toString
will not work, becauseprototype
property have only functions, andShape
in your example is object defined using literal object syntax – Łukasz Szewczak Commented Apr 23, 2014 at 9:17
2 Answers
Reset to default 5First example:
It will not work, because all the object literals ({..}
) inherit from Object.prototype
. And so, they will not have prototype
attribute (only the Function objects will have the prototype
attribute). So, Shape.prototype
will be undefined
and you are trying to create toString
attribute on undefined
, which is not possible. That is why it fails.
Second example:
This follows parasitic inheritance idea. The child constructor function, first constructs an object with the Parent's constructor function. Then it augments it to its needs and returns the customized object of type Parent. Since the object is of parent's type, it still can access the functions and data in parent's prototype object.
Quoting from Parasitism wikipedia page,
relationship between species, where one species, the parasite, benefits at the expense of the other, the host.
Here, the child is the parasite and it makes use of the parent constructor (host).
Note : The return statement in the child's constructor is very important. It has to return the newly constructed object. Otherwise, an newly constructed object of child's type will be returned, by default.
Javascript uses the Prototype model for inheritence. This means that you can create a new object by copying an existing one and just adding properties; the prototype for one object is just another object.
In most OO languages, like C++, Java and so on, there is a strict system of classes and instances; if you want a Bar that is just like a Foo but has a Hat property, you have to define a class Bar which inherits from Foo (or contains an instance of Foo), and then instantiate the class to create a new BarObject. By contrast in Javascript, all you have are instances, and to create a new type you just add stuff to an existing one.
In your example, the Employee constructor returns a Person object that is has modified to add a group
property to, instead of returning a new Employee object. It has parasitically inherited from Person, but been extended with this new property.