I am a new programmer, and I started in C and am now starting to enjoy JavaScript and a tiny bit of PHP more. Lately I've heard the terms 'private' and 'public' functions a lot. Could anybody give an explanation of the both and how they are of use to a programmer?
And I'm probably totally wrong here... but is a
(function(){})
in javascript a private function?
I am a new programmer, and I started in C and am now starting to enjoy JavaScript and a tiny bit of PHP more. Lately I've heard the terms 'private' and 'public' functions a lot. Could anybody give an explanation of the both and how they are of use to a programmer?
And I'm probably totally wrong here... but is a
(function(){})
in javascript a private function?
Share Improve this question edited Jul 13, 2010 at 12:00 ChrisF♦ 137k31 gold badges264 silver badges334 bronze badges asked Apr 15, 2010 at 19:49 Kyle HotchkissKyle Hotchkiss 11.1k20 gold badges59 silver badges82 bronze badges 2-
3
function(){}
is an anonymous function. – Gumbo Commented Apr 15, 2010 at 19:51 - Oh dang! I've got so much to learn! – Kyle Hotchkiss Commented Apr 15, 2010 at 20:05
1 Answer
Reset to default 11The terms "public" and "private" aren't really appropriate with Javascript. The key is this: do you have a way to refer to some function you know to exist? That is, can you refer to it by name (either directly or as a property of another object you can reference)? If so, congratulations, you can call the function. If not, then you can't.
What this means is that the "publicness" of a function is not a static thing as it is in something like Java or C. Consider:
var x = (function() {
function maybePrivate() { return "my privates!"; }
return {
getMyPrivates: function() {
var rv = maybePrivate;
this.getMyPrivates = function() { return null; };
return rv;
}
};
})();
Weird. So now "x" is an object, and you can see that there's a "maybePrivate" function in there. However, that name — "maybePrivate" — doesn't do you any good, because from outside x you can't use the name to get to the function.
However, that weird object has another function, called "getMyPrivates", and you can get to it. When you call it, you get back a reference to the "maybePrivate" function, which you can then use to call that function. Now, is "maybePrivate" still private? Well, if you do this:
alert(x.getMyPrivates()());
you'll see the "my privates!" message. So it looks like "maybePrivate" isn't so private anymore. However, if you try that again, it won't work, because "getMyPrivates" arranges for itself to be dummied out after it is called the first time.
The point of that little trip through the looking glass is to illustrate that Javascript is dynamic. The only thing that's really "static" is that once you've sealed up a function inside a closure (like "maybePrivate" in the above example), you can't force it to be revealed from outside the object (at least, not without going through some really twisty contortions; the language is so mushy that it's dangerous to claim that something is "impossible" :-). That object in the example could choose to reveal its "private" stuff, but it can also choose not to.