Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8820979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:44:29+00:00 2026-06-14T05:44:29+00:00

I need to do the following: function Shape (width, height) { var self =

  • 0

I need to do the following:

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());

Both calls to the base prototype method must return the area of the caller, but without overriding the method.

How can I make it return (width * height) / 2 for triangle and (radius * radius) * pi for circle without overriding it?

Thanks for your time and all best!

EDIT:

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 < 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: <b>" + rectangle.width + "</b> height: <b>" + rectangle.height + "</b> area: <b>" + rectangle.calculateSurface() + "</b><br />";
    outputArr[i + 1] = triangle;
    outputStr += "Triangle base: <b>" + triangle.width + "</b> height: <b>" + triangle.height + "</b> area: <b>" + triangle.calculateSurface() + "</b><br />";
    outputArr[i + 2] = circle;
    outputStr += "Circle radius: <b>" + rectangle.width + "</b> area: <b>" + circle.calculateSurface() + "</b><br /><br />";
};

output.innerHTML = outputStr;

It works correctly, I hope I understood it right.
Thanks again!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:44:30+00:00Added an answer on June 14, 2026 at 5:44 am

    First of all, you shouldn’t define method (calculateSurface) of the custom type (a.k.a. “class”) on the instance, so, not:

    self.calculateSurface = function () {
        return (self.width * self.height);
    };
    

    But:

    Shape.prototype.calculateSurface = function () {
        return (this.width * this.height);
    };
    

    Then you need to shadow the property that is higher in the prototype chain. So the lookup will first look at Triangle.prototype and only then at Shape.prototype – using the first property it finds.

    Triangle.prototype = new Shape;
    // Note that you don't need to put parens after Triangle.
    // You only set property to point at constructor function itself,
    // not to the value constructor returns when invoked. (Also, constructor 
    // should be invoked with `new` operator, not just with parens).
    Triangle.prototype.constructor = Triangle; 
    // now shadowing Shape.prototype.calculateSurface
    Triangle.prototype.calculateSurface = function () { 
      // custom code here
    }
    

    Same goes for the Circle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can someone help with this? I need the following function to do this... $x
I need to call a function in the following fashion: var f = 'fadeOut';
I need following function (from C++ dll) available in C++/CLI extern C _declspec(dllexport) void
I need the following C function in Python: int func(Uint8 *bytRecvBuffer, int *iRecvLen); I
I need to sort string, and I came up with the following function. def
I need to check if an object contains any value. The following function works
I need a function with the following signature: System.Reflection.PropertyInfo getPropertyInfo(System.Type type, string NavigationPath) or
I need to add fade function in the following script. I need that the
I need a VBA function that will perform the following: Find all unique values
Morning- I need a function that would produce the following kind of looking sequence:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.