I'm a newbie in the JavaScript world, and I came up with this weird problem when i was attempting prototype chaining inheritence.
I have 3 classes
//class parent
function parent(param_1){
this.param = param_1;
this.getObjWithParam = function(val){
console.log("value in parent class "+val);
console.log("Constructor parameter : "+this.param);
};
};
//class child
function child(param_1){
this.constructor(param_1);
this.getObjWithParam = function(val){
console.log("value in child class "+val);
val = Number(val)+1;
child.prototype.getObjWithParam.call(this, [val]);
};
};
child.prototype = new parent();
//class grandChild
function grandChild(param_1){
this.constructor(param_1);
};
grandChild.prototype = new child();
var gc = new grandChild(666);
gc.getObjWithParam(0);
Firstly, I wanted to pass a parameter to the constructor of the parent classes, like the way they do by calling super(args) in other OO languages.
so this.constructor(param_1);
fairly suits the purpose.
However, the output comes up as
value in parent class 0
Constructor parameter : 666
Which suggests, that the class grandChild has skipped the prototype chain, and instead of calling getObjWithParam() of child() class, has called getObjWithParam() of the parent class.
Does anyone have any idea what goes wrong here?
NB: 2 more findings i want to add, and the second one is the important one. --> If I try to find the constructor of grandChild class by
console.log(gc.constructor)
the output i get is
function parent(param_1){
this.param = param_1;
this.getObjWithParam = function(val){
console.log("value in parent class "+val);
console.log("Constructor parameter : "+this.param);
};
}
which is the not quite the thing i expected. I was expecting to see the child class.
--> If I try to comment //this.constructor(param_1);
in the child() and the grandChild() class, the code works exactly as expected.
Could anyone explain this phenomenon please.
Also, it'll be highly appreciated if anyone could suggest an workaround.
Thanks
That means all the objects in JavaScript, inherit the properties and methods from Object. prototype. This is called Prototype chaining. This is a very powerful and potentially dangerous mechanism to override or extend object behavior. Objects created using new keyword, inherit from a prototype called Object.
When it comes to inheritance, JavaScript only has one construct: objects. Each object has a private property which holds a link to another object called its prototype. That prototype object has a prototype of its own, and so on until an object is reached with null as its prototype.
Short answer So what's the difference between constructor and prototype? A short answer is that the constructor is a function that is used to create an object, while the prototype is an object that contains properties and methods that are inherited by objects created from a constructor.
prototype is a property of a Function object. It is the prototype of objects constructed by that function. __proto__ is an internal property of an object, pointing to its prototype.
Declaring a this.SOME_METHOD in the constructor function doesn't add it to the type's prototype. Prototype functions need to be declared separately, e.g.:
//class parent
function parent(param_1){
console.log("parent " + param_1);
this.param = param_1;
}
parent.prototype.getObjWithParam = function(val) {
console.log("value in parent class "+val);
console.log("Constructor parameter : "+this.param);
};
//class child
function child(param_1){
console.log("child " + param_1);
this.constructor(param_1);
}
child.prototype = new parent();
child.prototype.getObjWithParam = function(val) {
console.log("value in child class "+val);
val = Number(val)+1;
parent.prototype.getObjWithParam.call(this, [val]);
}
//class grandChild
function grandChild(param_1){
console.log("grandChild " + param_1);
this.constructor(param_1);
}
grandChild.prototype = new child();
var gc = new grandChild(666);
gc.getObjWithParam(0);
I would recommend you to read this article, to get a deeper insight how prototypes work in javascript.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With