HTML and CSS Reference
In-Depth Information
this.radius = radius;
// Assigning the functions to properties of the prototype
// makes them public methods
circleProto.getRadius = getRadius;
circleProto.setRadius = setRadius;
In Listing 7.41 we create an anonymous closure that is immediately executed
with Circle.prototype as the only argument. Inside we add two public meth-
ods, and keep a reference to one private function, ensureValidRadius .
If we need a private function to operate on the object, we can either design it to
accept a circle object as first argument, or invoke it with,
/* args... */) , thus being able to refer to the circle as this inside the
We could also have used the existing constructor as the enclosing scope to hold
the private function. In that case we need to also define the public methods inside
it, so they share scope with the private function, as seen in Listing 7.42.
Listing 7.42 Using a private function inside the constructor
function Circle(radius) {
this.radius = radius;
function ensureValidRadius(radius) {
return radius >= 0;
function getRadius() {
return this.radius;
function setRadius(radius) {
if (ensureValidRadius(radius)) {
this.radius = radius;
// Expose public methods
this.getRadius = getRadius;
this.setRadius = setRadius;
Search WWH ::

Custom Search