Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to alter a prototype's method without overriding it?
    text
    copied!<p>I need to do the following:</p> <pre><code>function Shape (width, height) { var self = this; self.width = width; self.height = height; self.calculateSurface = function () { return (self.width * self.height); }; }; function Circle (radius) { //make sure width == height == radius. }; Circle.prototype = new Shape; Circle.prototype.constructor = Circle(); function Triangle (base, height) { var self = this; Shape.apply(self, arguments); }; Triangle.prototype = new Shape; Triangle.prototype.constructor = Triangle(); var circle = new Circle(5); var triangle = new Triangle(5, 8); alert(circle.calculateSurface()); alert(triangle.calculateSurface()); </code></pre> <p>Both calls to the base prototype method must return the area of the caller, but without overriding the method.</p> <p>How can I make it return (width * height) / 2 for triangle and (radius * radius) * pi for circle without overriding it?</p> <p>Thanks for your time and all best!</p> <p><strong>EDIT:</strong></p> <pre><code>var output = document.getElementById('output'); function Shape(width, height) { var self = this; self.width = width; self.height = height; }; Shape.prototype.calculateSurface = function () { return (this.width * this.height); }; function Rectangle(width, height) { Shape.apply(this, arguments); }; Rectangle.prototype = new Shape; Rectangle.prototype.constructor = Rectangle; function Triangle(base, height) { Shape.apply(this, arguments); }; Triangle.prototype = new Shape; Triangle.prototype.constructor = Triangle; Triangle.prototype.calculateSurface = function () { return ((this.width * this.height) / 2); }; function Circle(radius) { Shape.apply(this, arguments); }; Circle.prototype = new Shape; Circle.prototype.constructor = Circle; Circle.prototype.calculateSurface = function () { return((this.width * this.width) * Math.PI); }; // testing var outputStr = ""; var outputArr = []; for (var i = 0; i &lt; 30; i++) { var rectangle = new Rectangle(i + 1, i + 2); var triangle = new Triangle(i + 1, i + 2); var circle = new Circle(i + 1); outputArr[i] = rectangle; outputStr += "Rectangle width: &lt;b&gt;" + rectangle.width + "&lt;/b&gt; height: &lt;b&gt;" + rectangle.height + "&lt;/b&gt; area: &lt;b&gt;" + rectangle.calculateSurface() + "&lt;/b&gt;&lt;br /&gt;"; outputArr[i + 1] = triangle; outputStr += "Triangle base: &lt;b&gt;" + triangle.width + "&lt;/b&gt; height: &lt;b&gt;" + triangle.height + "&lt;/b&gt; area: &lt;b&gt;" + triangle.calculateSurface() + "&lt;/b&gt;&lt;br /&gt;"; outputArr[i + 2] = circle; outputStr += "Circle radius: &lt;b&gt;" + rectangle.width + "&lt;/b&gt; area: &lt;b&gt;" + circle.calculateSurface() + "&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;"; }; output.innerHTML = outputStr; </code></pre> <p>It works correctly, I hope I understood it right. <strong>Thanks again!</strong></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