One core thing about JavaScript that tends to confuse people is how to get private, public, and privileged members. First off one might even question the fact of whether or not they're useful or needed - and to one degree, they're not. But like any OOP language that allows for declaration of private and public members, there's not even a need for them there as well. The fact of the matter is that it helps abstract your application logic in a way that lets you as the publisher of "whatever it is that you're building" a way to expose which functions are usable to the outside, and those that are just internal interface methods (yes, those functions that do nothing as far as management is concerned).
Granted, I've written on this topic briefly when talking about
JavaScript namespaces but didn't get into full detail on how to achieve all three styles, and how it all actually works.
So, let's get a working code block and demonstrate all three states of
private, public, and privileged.
Sample Class Structure
// Can't forget my namespace
var DED = window.DED || {};
DED.Class = function() {
var fn = function(e, id, param, o) {
// process arguments here
};
return {
init : function() {
var o = DED.Class.get('el');
fn.apply(o, arguments);
},
get : function(id) {
return document.getElementById(id);
}
};
}();
Sweet, so as we can see, this is a very basic structure that gives us some class-like structure which enables us to group methods into a container. Some core things to notice first off is the self-invoking structure which immediately returns an object for public use. That's shown in these core areas:
Self-invoking hi-lighted
DED.Class = function() {
var fn = function(e, id, param, o) {
// process arguments here
};
return {
init : function() {
var o = DED.Class.get('el');
fn.apply(o, arguments);
},
get : function(id) {
return document.getElementById(id);
}
};
}();
Although this may look
awkward at first pass, it's what's known as an immediate self-invoked function - and in our case, we're returning an object at declaration-time. Unda-stood?
What's Private
These are simply the declared members at the top of the function which can only be internally accessed from other members that share its
scope. There is one private member in our class which hi-lighted below:
Private members
DED.Class = function() {
var fn = function(e, id, param, o) {
// process arguments here
};
return {
init : function() {
var o = DED.Class.get('el');
fn.apply(o, arguments);
},
get : function(id) {
return document.getElementById(id);
}
};
}();
One quick way to tell that this is a private method is that it simply can't be accessed from the global playground area via
DED.Class.fn() since
fn is not defined in the global scope.
What is public
The public is the world outside your office that has real people you can interact with. This includes friends, family, even neighbors.... Public members are accessible outside your Class. In our example, we actually have two public methods, but only one that is entirely public. That method is hi-lighted below:
Public members
DED.Class = function() {
var fn = function(e, id, param, o) {
// process arguments here
};
return {
init : function() {
var o = DED.Class.get('el');
fn.apply(o, arguments);
},
get : function(id) {
return document.getElementById(id);
}
};
}();
It only makes sense to use the classic
get() as our token public example.
And what's privileged
As you might have already guessed by process of elimination, it is our
DED.Class.init which is not only public in the sense that it is accessible from the outside world, but it's privileged because it is accessing a private member. Of course we can't do anything notty with the private member it's using - but rather all you get is special privileged access to borrow its functionality. You could also of course entirely wipe out init by just doing the following:
wipe out the init
DED.Class.init = null;
However that still doesn't let you get at that private
fn method that other members may also be borrowing.
Why does all this matter?
Well, it's more for educational purposes. Do as you will with it. In fact this post is very unlike me, especially without a demo that makes things fly across the screen. I apologize if I didn't meet the buzzword index criterion.
If this is all gibberish to you - don't feel bad. Even
Jonathan Snook rarely feels the need to go all out on separating private and public members - especially if it's just you, yourself, and your blog.
References