Defining objects inside self-invoked anonymous functions
In our previous episode, we saw a canonical way to create objects in JavaScript that suggested code reuse and facilitated automatic object initialization, but it didn’t let you implement private attributes nor did it go out of its way to keep from polluting the global namespace.
What follows is the simplest way I’ve seen that accomplishes all of the above. And boy is it a ride! We are going to create a self-invoking anonymous function and inside the function we will define the object and create a single point of connection to the window
with a closure.
Here we go:
// == Begin definition ============================== (function(window) { // -- Constructor --------------------------------- var Clicker3 = function() { // -- Private properties -- var _numClicks = 0; // -- Private methods -- // Methods defined in this scope and not on // 'this' are private'. var _update = function() { console.log("Clicker3 obj: " + _numClicks); }; // -- Public methods -- // Methods defined on 'this' are public, can // access private and public members. this.click = function () { _numClicks++; _update(); }; this.reset = function () { _numClicks = 0; _update(); }; this.getNumClicks = function () { return _numClicks; }; // -- Initialize state -- this.reset(); // reset is redundant here; // for demo only. _update(); }; // !Clicker3() // -- "Hyper-public" methods ---------------------- // Methods defined on the prototype can only access // public members! Clicker3.prototype.showInfo = function() { alert("I am a Clicker3 object who is at " + this.getNumClicks() + " clicks."); }; // -- Publish ------------------------------------- // Expose Clicker3 (and only Clicker3) to the // global window (i.e., make it public). window.Clicker3 = Clicker3; }(window)); // == End definition ================================
The self-invoking function creates a new scope in which everything is defined. Passing in window
as a parameter allows easy access to global window from within that new scope. This setup lets you:
- Prevent pollution of global namespace.
- Expose what you want to the global window and keep the rest private.
So, now we have something that:
- Has, “Reuse me!” written all over it.
- Allows for automatic object initialization.
- Lets you fully encapsulate objects.*
- Protects the global namespace.
But it’s not totally cool because none of the above are code-level constructs. In other words, it’s neither readable or writable. JavaScipt is/isn’t fun. :-\
Just as a reminder, I am not (yet) taking on the following:
- Inheritance-like stuff
- “Class” (i.e., static) members*
- Polymorphism
*An issue of religious importance.