Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript mutator issue in pseudo class
    text
    copied!<p>Here is the code:</p> <pre><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; }; }; </code></pre> <p>However I cannot get the this.setRadius() function to successfully mutate the private properties. For instance, I run this:</p> <pre><code>var circle = new Circle(5); alert(circle.area); circle.setRadius(10); alert(circle.radius); </code></pre> <p>And it outputs 78.blah which is correct, then it outputs a radius of 5.</p> <p>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?</p> <p>Thanks!</p> <p>EDIT:</p> <p>Alright, thanks for why the radius isn't working correctly. That is a little obvious. As pointed out, I do not ever update this.radius. However, the point remains that the _area and _perimeter values are not updated either.</p> <p>I run:</p> <pre><code>var circle = new Circle(5); alert(circle.area); circle.setRadius(10); alert(circle.area); </code></pre> <p>And I still get the old area even after updating the radius. Note that the _area function is dependent on _radius and not this.radius.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload