1

In this example:

var circle = {
  radius : 9,
  getArea : function()
  {
    return (this.radius * this.radius) * Math.PI;
  }
};

from this page's Encapsulation topic, the getArea is private, how would be with it public?

2
  • 4
    are you sure? Because that's public. I can do circle.radius and circle.getArea() Commented Dec 21, 2010 at 16:46
  • Why don't you try it in firebug your self because I did and it sure works for me :) Commented Dec 21, 2010 at 16:50

3 Answers 3

7

That's not JSON notation, that's JavaScript object literal notation. (JSON is a subset of object literal notation. It doesn't allow functions, requires double quotes, and doesn't support octal or hex.)

getArea isn't private, anything can call it. The page you quoted is simply incorrect. If you want patterns for truly private methods in JavaScript, here's a roundup with desciptions of the various trade-offs (including the memory cost of the Crockford model, which is the most common form).

Sign up to request clarification or add additional context in comments.

4 Comments

@Tom: LOL, I was just commenting that the syntax wasn't quite right. It's still not private.
So this page's Encapsulation topic is completely wrong?
@Tom: Admittedly I just scrolled down to that example on it, but yes, my read is that it's just wrong. It says "In both cases this limits the scope of each getArea() method to each circle object." which is simply untrue. It's certainly wrong about "scope". Even if you try to read "scope" as meaning "context" (this value), which is a pretty big leap, it's still wrong; JavaScript doesn't have methods, just functions: blog.niftysnippets.org/2008/03/mythical-methods.html
+1 for the blog post -- i've only skimmed it but it looks like a nice in-depth study of pseudo-private data design patterns in Javascript.
1

As T.J. Crowder says... that's not JSON. I think maybe the problem is that the getArea property of your circle object is being set to whatever the getArea function returns. Which means that circle.getArea() won't work because circle.getArea is a number. This is wrong but I think its a step closer to what you are trying to achieve (I'll show a better way after this):

var circle = {
  radius : 9,
  getArea : function(){
    return Math.PI * 9 * 9;  
  }
};

alert(circle.radius);
alert(circle.getArea());

The important thing above is to note the way I define the getArea function inside the object. This can be useful, but the problem is that the get area function doesn't have access to the radius variable, because objects like circle don't have their own scope. If you try :

var myObj = {test:this};

alert(myObj.this);

You'll see [object DOMWindow]

So how do you create an object Circle with a radius property and a getArea method? There are a many ways. One way is like this:

function Circle(r){
   var c = this;
   this.radius = r;
   this.getArea = function(){
     return Math.PI * c.radius * c.radius;
   }
}

var circle = new Circle(10);
document.write(circle.getArea()+"<br/>");

circle.radius = 20;

document.write(circle.getArea()+"<br/>");

var otherCircle = new Circle(1);
document.write(otherCircle.getArea()+"<br/>");

I've created a jsFiddle of the above.

This can also be achieved using prototype property:

function Circle(r){
   this.radius = r;
}

Circle.prototype.getArea = function(){
   return Math.PI * this.radius * this.radius;
}

var circle = new Circle(10);
document.write(circle.getArea()+"<br/>");

circle.radius = 20;

document.write(circle.getArea()+"<br/>");

var otherCircle = new Circle(1);
document.write(otherCircle.getArea()+"<br/>");

Comments

0

There are no such thing as private method or property in javascript.

Everyting is public but common practise is to treat anything beginning with _ as private, but thats only convention, not enforced byt the language.

To have private methods and properties you need real classes to define them and javascript does not have real classes.

4 Comments

you can create prive methods and properties with javascript. It's not like it is in java or other OO languages, but it does work. Have a look : crockford.com/javascript/private.html
"There are no such thing as private method...in javascript." True, but because JavaScript doesn't have methods at all (just functions). It's entirely possible to create private functions that nothing else can see. There are costs, though. "There are no such thing as private...property in javascript." Quite true. Once something is actually a property on an object, it's visible to anything that can reference that object.
@Zevan: You can't create private properties in JavaScript, not in the literal sense of a property on an object. But yes, you can certainly have private data, as the Crockford link you provided demontrates.
+1 That's a good point, I guess if you really get down to the semantics of it you are right. Always good to know how to emulate this kind of behavior like in the Crockford example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.