33. Methods


#1

Hey!

Following from the code:

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

I have written:

var earth = new Circle (6371);

var earthradius = earth.area();
var earthperimeter = earth.perimeter();
console.log(earthradius, earthperimeter);

Although this works, is there an easier way to call and log the radius and perimeter method for the circle object earth that I have created.

Thanks heaps and happy new year!!

Tim


#2

You do not need to store values in variables before printing them


#3

Method #1: Create another method which takes a parameter and returns the function you want. This is easier to type and doesn't require assigning methods unnecessarily to global variables, while also being flexible.

earth.call = function(par) {
    switch (par) {
        case 'a':
            this.area();
            break;
        case 'p':
            this.perimeter();
            break;
        default:
            return 'Wrong Call Value.';
            break;
    }
}
earth.call('a') //This will return your area function
//Notice you don't need to re declare a return because it exists in the function

Method #2: Probably more likely and practical, you don't need to create functions for a single line of code. Area will always be area, and perimeter will always be perimeter. Apply the math within the function to the variables and they will always contain the perimeter of the radius, and so on for the area.

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

You're declaring functions simply to return a value. Functions are reserved for blocks of code, you can very well do logic in a variable and it'll return your result no different. If not, then any other logic in a variable such as . . .

var a = 2;
var b = 4;
var sum = a + b;
var statement = 'By combining '+a+' and '+b+' we see how both math and string concatenation are logical operations which can be done on a single line of code and thus are viable in use within a variable.';

. . . would be rendered null.

Now allow me to explain this for the sake of correction:
applying the values from the methods into new global variables is pointless unless for some reason you need to store them before changing them.

Let's say an asteroid is about to strike the earth, well only then would you do something like

var oldEarthRadius = earth.area();

What you're doing now is just unnecessary work. Methods/properites of objects are nothing more then specific functions/variables for a parent variable. So taking that child and extracting it to an external variable? It serves no purpose. Already you save yourself a great deal of work.

There is no reason to create var eartradius = earth.area(); When earth.area(); does not change.

This is opposite of the purpose of methods. They're easy to call and already belong to identifiers, you're creating more work by making another identifier for an identifiers' children.


#4

@emgo_dev

Sorry I am new to all this. If I had the 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 Math.PI * 2 * this.radius;
};
};

And I wanted to create the circle object earth with the radius of 6371, and also have the area and perimeter logged, what code would I have to write? Could I get the code please.

Thanks


#5

That would be Method #2 which is listed in my last comment. The earth.area() is now earth.area, it's no longer a function, because your math is done on a single line of code in a property/variable, and because it's a variable, it returns the result of any logic done within it, where as a function (which I spoke about above) is reserved for running logic in a block of code, and does not return a value on it's own.


#6

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 Math.PI * 2 * this.radius;
};
};

var earth = new Circle (6371);
console.log(earth.area, earth.perimeter);

I'm not sure if you are telling me to write it like this but with console.log(earth.area, earth.perimeter), [function] [function] is logged out instead of the area and perimeter.

When I write console.log(earth);, [function] is also printed out instead of the area and perimeter.

Could you please advise.

Sorry


#7

You're not changing your object constructor at all, that's why. What I said assumed that you were going to use my code.

Your earth.area is still a function so you need those parenthesis, earth.area(), my code in Method #2 in my post above does not store a function, so it isn't a method, it stores the math in a normal property, so earth.area does not need parenthesis.

Just use this code below and you'll be fine:

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

#8

Thanks @emgo_dev

Changing the constructor worked - I was able to log out the area and perimeter with console.log(earth);. I was just using the constructor that they gave me.

This may be a silly question but what is the point of having the area and perimeter as a function opposed to a property?


#9

For the purposes of that exercise, I can only assume it is because it's consistent with the teaching they were presenting to you throughout the course.

Feel free to message me if you have any more questions about this.