Welcome to the Linux Foundation Forum!

Prototypal Inheritance (Constructor Functions)

When using Constructor Functions, why you need to use another function to assign a prototype to an object instead of assigning the value of the desired parent explicitly. In https://trainingportal.linuxfoundation.org/learn/course/nodejs-application-development-lfw211/key-javascript-concepts/key-javascript-concepts?page=4

Why we have to use

function inherit (proto) {
function ChainLink(){}
ChainLink.prototype = proto
return new ChainLink()
}

and execute

Cat.prototype = inherit(Lynx.prototype);

to assign Lynx as the prototype of Cat and the following just dont work:

Cat.prototype = Lynx.prototype;

Answers

  • @nayib - the former is creating a prototype chain, the latter is replacing a prototype.

    So in the former you have Cat.prototype -> -> Lynx.prototype

    in the latter you just have Lynx.prototype

Categories

Upcoming Training