Methods


#1

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*radius;
    };
};

What type of object definition is this ?? Codecademy says Circle is an Object here while as per the rules Circle here is a constructor. Please suggest.


#2

In specific terms, it is an object, an instance of the Function class. In general terms, it is a constructor, but then any function can be a constructor just by invoking it with new. JavaScript is very flexible that way.

Examine Circle as a function with a return value

Lots and lots of stuff attached to this. When we use new to invoke the constructor (and do not return anything), this is focused only on the direct attributes and methods in the constructor object. That's what the instance will own.


#3

In loose terms, a function is a string object that just happens to be a property of a really cool class.

console.log(Circle.toString());

which reveals,

function Circle(radius) {
    this.radius = radius;
    this.area = function () {
        return Math.PI * this.radius * this.radius;
    };
    this.perimeter = function(){
        return 2*Math.PI*radius;
    };
}