I'm on my journey to learn Object-Oriented Programming in Javascript. I got this video lession from here http://www.objectplayground.com/ which I understood quite a bit the prototypal method over classical method.
While watching the lession, I was paused by the example shown for the classical method to work with subclasses which is the below:
//superclass
function Answer(value){
this._val = value;
}
//define prototype property 'get' for the superclass
Answer.prototype.get = function fn1(){
return this._val;
}
//subclass
function FirmAnswer(value){
Answer.call(this,value);
}
FirmAnswer.prototype = Object.create(Answer.prototype);
FirmAnswer.prototype.constructor = FirmAnswer;
//define prototype property 'get' for subclass
FirmAnswer.prototype.get = function fn2(){
return Answer.prototype.get.call(this);
}
var luckAnswer = new FirmAnswer(7);
luckAnswer.get(); //7
Question:
From my understanding of call
function, it will set the this
to the current context which is for example from this line Answer.call(this,value)
from the FirmAnswer
function, so the _val
from Answer
will be set for the FirmAnswer
and not for the Answer
(please correct me if I'm wrong).
So it leads me to my confusion if the above analyzation is correct, as to why the get
property of the FirmAnswer.prototype
returns Answer.prototype.get.call(this)
and not just Answer.prototype.get()
alone since the this
is already set to FirmAnswer
when calling new FirmAsnwer(7)
?
Please shed me some light as I'm pretty confused as of the moment. I'm pretty sure I understand well the prototypal method but the classical method confuses me quite a bit.
Thank you in advance!