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?
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?
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).
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.htmlAs 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/>");
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.