Let??™s look at the ElementReady script:
var ElementReady={
polled:[], /* store polled elements */
timer:null, /* store timer */
timerStarted: false,
CHAPTER 3 n OBJECT-ORIENTED PROGRAMMING 71
ceasePoll:function(){...},
startPoll:function(){...},
check:function(clean){...},
cleanUp:function(){...},
chkDomId:function(elId,callback) {...}
};
In this object that you??™re using to handle all your tasks, most of these properties are
actually for internal use only. chkDomId(), cleanUp(), and check() are the only methods that
are ever used from outside the object. If that??™s the case, should the other properties actually
be accessible? Based on what you have learned about object creation and closures, you can
actually redesign this class so that only those three functions are accessible. Everything else
will be accessible from only those functions:
var ElementReady= new function(){
var polled = []; /* store polled elements */
var timer = null; /* store timer */
var timerStarted = false;
var ceasePoll = function(){...};
var startPoll = function(){...};
return {
check:function(clean){...},
cleanUp:function(){...},
chkDomId:function(elId,callback) {...}
}
};
Let me explain what was updated. The first thing I did was change the main ElementReady
object from an object literal to an anonymous function that is instantiated into a new object.
Now that it??™s no longer an object literal, the items within it are set up just like regular variables.
Pages:
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130