How to print methods


#1


https://www.codecademy.com/en/courses/spencer-sandbox/5/4?curriculum_id=506324b3a7dffd00020bf661


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

Completing the exercise was fine but I have been trying to print either the perimeter or the area but I am unsure how to do this. I know the radius needs to be defined and I have tried different things but all I get is either error messages or not what I want to happen. Any help would be great.


#2

first we must create an instance of our object:

circle = new Circle(3);

now we have an instance of our object (circle, we could call this anything we like)

now we can call methods on the instance of our object:

circle.perimeter();

This is nothing new what i do here, right?


#3

It hasn't covered instances of objects yet. That makes sense cheers .


#4

i love this answer:

Excellent question.

I'll explain it in the simplest way possible: Say you have 5 apples in your basket. Each of those apples is an object of type Apple, which has some characteristics (i.e. big, round, grows on trees).

In programming terms, you can have a class called Apple, which has variables size:big, shape:round, habitat:grows on trees. To have 5 apples in your basket, you need to instantiate 5 apples. Apple apple1, Apple apple2, Apple apple3 etc....

Alternatively: Objects are the definitions of something, instances are the physical things.

Does this make sense?

from this stackoverflow question.

Hopes this helps!


#5

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