I am in the know of learning object oriented programming in Javascript. I got this video from here http://www.objectplayground.com/ , which I understood quite a bit by the prototype method according to the classical method.
While viewing the abbreviation, I was stopped by the example shown for the classical method of working with subclasses, which is 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 the call function, he will set this to the current context, which, for example, from this line Answer.call(this,value) from the FirmAnswer function, so _val from Answer will be set for FirmAnswer , and not for Answer (please correct me , if I am wrong).
Thus, this leads me to my confusion if the above analysis is correct, why the get property for FirmAnswer.prototype returns Answer.prototype.get.call(this) , and not just Answer.prototype.get() , since this Already configured for FirmAnswer when calling new FirmAsnwer(7) ?
Please shed some light for me, as I'm rather confused. I am sure that I understand the prototype method well, but the classical method confuses me a bit.
Thank you in advance!
javascript prototype-programming prototype-pattern prototype-chain
Roljhon
source share