Search code examples
javascriptinheritanceprototype-programming

javascript inheritFrom Vs Prototype


I am wondering whats the difference between inheritFrom and the prototype when defining inheritance in Javascript.

function classA{}
classA.name="abc";
classA.functionName=function(){
alert("Function Name Alert");
}
function classB{ }

Whats the difference in the below codes?

classB.prototype=classA();

and

classB.prototype.inheritFrom(classA);

Solution

  • B.prototype.inheritFrom(A) is not standard JavaScript, whereas B.prototype = new A is standard JavaScript. I suggest learning the ins and outs of JavaScript and embracing the prototype. You'll be better off for knowing it. It's really not too difficult:

    function A(){}
    function B(){}
    B.prototype = new A;
    b = new B;
    console.log(b instanceof B, b instanceof A);
    //-> true, true