Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy people use prototype in javascript when it is easy to inherit using apply () and call () methods?
    text
    copied!<p>Shape is inherited by rectangle. This inheritance can be done by many methods. Here I have used apply() and call (). When draw method is child is called, from that method draw method of base class is called again. I have done this thing in two ways. One is making the prototype draw method of base class and the other one is by using apply() and call() method.<br> First Method :</p> <pre><code>function Shape () { this.name='Shape'; this.getName = function () { return this.name; }; this.draw = function () { alert("Something"); }; } function Rectangle () { Shape.apply(this); var X=this.draw; this.name = 'rectangle'; this.id=500; this.draw = function () { X.call(this); }; } </code></pre> <p>Second Method : </p> <pre><code>function Shape () { this.name='Shape'; this.id=100; this.getName = function () { return this.name; }; } Shape.prototype.draw = function() { alert("Something"); }; function Rectangle () { this.name = 'rectangle'; this.id=200; this.draw = function () { Shape.prototype.draw.call(this); }; } Rectangle.prototype = new Shape(); Rectangle.prototype.constructor = Rectangle; </code></pre> <p>Both of these two methods does the similar thing (in case of providing output). I know that by using apply () and call () method I can't directly get the access of base class's prototypes. Inheritance using apply() and call() seems less complicated to me. If both are same then why don't people use apply() and call() that much ? Why do I need to use prototypes ? What problem will I face if I don't use prototypes and inherit base classes using apply() and call () ??</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload