Introduction to Objects1/ Constructors with methods


#1

I'm just not seeing my error here. Any help is greatly appreciated.

Oops, try again. When the computer tried to call calcPerimeter of a Rectangle object, the following error was thrown: TypeError: rec_test.calcPerimeter is not a function

function Rectangle(height, width) {
  this.height = height;
  this.width = width;
  this.calcArea = function() {
      return this.height * this.width;
  }
  };
  function Perimeter(height, width) {
  this.height = height;
  this.width = width;
  this.calcPerimeter = function() {
      return (this.height * 2) + (this.width * 2); // not sure if this is correct //
      }
};

var rex = new Rectangle(7,3);
var area = rex.calcArea();
var perimeter = rex.calcPerimeter();.


#2

The perimeter method is meant to be a property of Rectangle.

function Rectangle(height, width) {
    this.height = height;
    this.width = width;
    this.calcArea = function() {
        return this.height * this.width;
    };
    this.calcPerimeter = function() {
        return (this.height * 2) + (this.width * 2);
    };
};

This can be factored:

(this.height * 2) + (this.width * 2)

2 * (this.height + this.width)

#3

thank you, that helped.


#4

Thanks. Helped me too.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.