The other answers here aren't bad, but they aren't getting to the real heart of your question.
In actual fact, there is no such thing as a private method in JavaScript. Mostly, this is because there is no such thing as a method in JavaScript. Not really.
Now before a bunch of people jump down my throat, let me clarify that: there's no such thing as a method in the pure sense of a classically object-oriented language. In a classical OO language, if you created an instance method, the only way to invoke that method is against an instance of the class. In JavaScript, you can attach functions to an object prototype, and they're treated as methods when invoked against an object that has that function in its prototype chain, but at the end of the day, that "method" is really just a function that acts like a method when its invoked a certain way.
You can look at this one of two ways: either JavaScript sucks because it doesn't support basic things like methods or JavaScript is awesome because you can have things that basically act like methods but can be used in more flexible ways too. I prefer the second perspective, but YMMV.
So, back to your question. The short answer is "JavaScript doesn't really have methods, so it can't really have private methods." The longer answer is "you can simulate private methods by exploiting the function scope of JavaScript." I say simulate because this approach doesn't really result in a method, but it does effect functionality hiding. To do this, you would use an immediately-invoked function expression (IIFE) to make sure the private function (remember: not really a method) isn't available externally:
var API = (function(){
var secrets = {};
var nextId = 0;
// in function scope here; anything declared here won't
// be available externally
function set(public, secret) {
// we can use 'this' here, but be careful!
// if we don't call this function correctly,
// 'this' will be bound to the global scope
// here we set a normal (public) instance variable
this.message = 'not secret: ' + public;
// here's how we can simulate a private "instance varaible"
secrets[this.id] = secret;
}
function API() {
this.id = nextId++;
}
API.prototype.public = function() {
// we can't call 'this.secret()' here because secret is
// not a method on this object
// we can call secret(), but that will not do what we want:
// it will break because the global scope doesn't have the
// variable we need
// we can, however, do this:
set.call(this, 'hello world!', 'i am secret');
}
API.prototype.tellSecret = function() {
return secrets[this.id];
}
return API;
})();
var api = new API();
api.public();
console.log(api); // won't print any secret info; we also
// can't call api.secret()
console.log(api.getSecret());
In this manner, we've simulated the hiding of data and functionality. Note that we had to give each API a unique ID. However, there is a vulnerability here: since id
is a public property of each instance of API, you could set it to another number, and access the secret information of another object! You could use Object.prototype.defineProperty
to lock down the id
property and close this vulnerability.
I hope this highlights some of the issues involved with hiding/restriction in JavaScript.