Social Icons

Pages

Featured Posts

Thursday, December 1, 2016

Introduction to Objects I 33/33

Methods
Methods are like functions that are associated with a particular object.
They are especially helpful when you want to either:
Update the object properties
Calculate something based on an object's properties.
Here, we have included a Circle object, with a radius property representing the circle's radius. We have implemented an areafunction which calculates the circle's area. Notice we have used Math.PI to get the π value.

Instructions
Define a method perimeter that calculates the perimeter of a circle.
function Circle (radius) {
    this.radius = radius;
    this.area = function () {
        return Math.PI * this.radius * this.radius;
    };
    this.perimeter = function () {
        return Math.PI * 2 * this.radius;
    };
}