0

Here is the code:

var Circle = function( radius )
{
    var _radius = radius,
    _area = function()
    {
        return Math.PI * _radius * _radius;
    },
    _perimeter = function()
    {
        return 2 * Math.PI * _radius;
    };

    this.radius = _radius;
    this.area = "Area" + _area();
    this.perimeter = "Perimeter" + _perimeter();
    this.setRadius = function( radius ) {
        _radius = radius;
    };
};

However I cannot get the this.setRadius() function to successfully mutate the private properties. For instance, I run this:

var circle = new Circle(5);
alert(circle.area);
circle.setRadius(10);
alert(circle.radius);

And it outputs 78.blah which is correct, then it outputs a radius of 5.

Any suggestions would be greatly appreciated! I thought that since the _area property is a function, it would recalculate each time, but I guess it's not? but why is this.radius not returning the new radius when it is obviously being reset?

Thanks!

1
Contributor
1
Reply
5
Views
4 Years
Discussion Span
Last Post by PhilEaton
0

Thanks guys, I decided to do it the following way:

var Circle = function( radius )
{
    var _radius = radius;

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

It works now, so all's good!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.