33. Methods - Help with calling the function


#1

Finished the exercise, and for kicks, I'm now trying to actually call the function and calculate the circle area and perimeter.

Code:

function Circle (radius) {
    this.radius = radius;
    this.area = function () {
        return Math.PI * this.radius * this.radius;
        
    };
    // define a perimeter method here
    this.perimeter = function() {
        return 2 * Math.PI * this.radius;
    };
}

var newCircle = new Circle(5);
console.log(this.perimeter, this.area);

I used newCircle to construct an object, and gave it a radius of 5, but I can't actually log the area and radius to the console.

Both return as "undefined".

Help please?


#2

Instead of this.perimeter use newCircle.perimeter, same goes for area.


#3

Nope, doesn't work. That just returns the function:

function () {
      return Math.PI * this.radius * this.radius;
      
  }
function () {
      return 2 * Math.PI * this.radius;
  }

This is what I'm getting in the console...


#4

That is the default output when you console print a variable. The right way to make the call is

console.log(newCircle.perimeter());
console.log(newCircle.area());

Btw, this is what is used extensively in AngularJS within controllers and services.


#5

That did it, thanks. Makes total sense as well as they are both functions, and need to be called.