Constructors With Methods eror


#1



Pop up window: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
Console:ReferenceError: calcPerimeter is not defined

Wach wor i dont get it


function Rectangle(height, width) {
  this.height = height;
  this.width = width;
  this.calcArea = function() {
      return this.height * this.width;
  };
  // put our perimeter function here!
  function calcPerimeter(height, width) {
      return this.height*2 + 2*this.width;
  }
};

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


#2

Well calcPerimeter only exists in the scope of Rectangle. If you want it to be available to objects you need to use

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

Or if you want to use it without any object as you tried here:

var perimeter = calcPerimeter();

then it should not be part of a constructor but a stand-alone function. Otherwise you might need to do something similar to var area = rex.calcArea();


#3

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