MSDN Magazine - December 2007 - (Page 37) Figure 2 Prototype-Based Version of MsdnMag.Timer // // MsdnMag.Timer :: prototype model // // Define the namespace the following class(es) belong to Type.registerNamespace(“MsdnMag”); // Class constructor:: Initializes internal members // // interval :: interval of the timer in milliseconds // callback :: JS function to execute periodically // resetCallback :: JS function to execute when the timer is stopped MsdnMag.Timer = function(interval, callback, resetCallback) { this._interval = interval; this._timerID = null; this._userCallback = callback; this._resetCallback = resetCallback; // Define a delegate to invoke the user-defined callback this._callback = Function.createDelegate(this, this._callbackInternal); this._resetCallback !== null) this.start(); } // Official end-point to start the timer function MsdnMag$Timer$start() { this._timerID = window.setTimeout(this._callback, this._interval) } // Official end-point to stop the timer function MsdnMag$Timer$stop() { window.clearTimeout(this._timerID); this._timerID = null; // Execute the user-defined clear function if (typeof(this._resetCallback) !== “undefined” && this._resetCallback !== null) this._resetCallback(); } } // Internal wrapper for the user-defined callback function MsdnMag$Timer$_callbackInternal() { // Invoke the user-defined callback if (this._userCallback !== null) this._userCallback(); // Restart the timer if a reset-callback is provided if (typeof(this._resetCallback) !== “undefined” && // Define the public interface of the class MsdnMag.Timer.prototype = { start : MsdnMag$Timer$start, stop : MsdnMag$Timer$stop, _callbackInternal : MsdnMag$Timer$_callbackInternal } // Register the class with the Microsoft AJAX Library framework MsdnMag.Timer.registerClass(“MsdnMag.Timer”); all instances of the object. The Microsoft AJAX Library is entirely written according to the prototype model and Microsoft strongly recommends that you use that model for your own objects. On the other hand, the Microsoft AJAX Library is just an abstraction layer on top of JavaScript, and JavaScript supports both closures and prototypes. Therefore, the choice between the programming styles is yours. So what’s the benefit of using the Microsoft AJAX Library? The library extends the JavaScript programming environment by adding a number of predefined objects that provide new language features such as namespaces, enumerated types, delegates, stronger typing, and inheritance. Figure 2 shows the MsdnMag.Timer object written as a fully qualified Microsoft AJAX Library object. The sample timer class accepts up to three arguments in the constructor to initialize the interval of the timer and the callback to run periodically. The timer is automatically restarted until explicitly stopped and a reset callback is also provided. Note that when you omit a declared parameter in a JavaScript function call, the actual value of the formal parameter evaluates to an undefined type. For this reason, you should check for both null values and undefined types. The following code shows a safe way to check for null-ness of JavaScript parameters: if (typeof(this._resetCallback) !== “undefined” && this._resetCallback !== null) { } programming interface. As long as you want it to be a property, and not just a public data container like a field, you need to read and write property values using ad hoc methods. The convention in use is get_XXX and set_XXX, where XXX is the name of the property. A sample implementation for an Interval property on the MsdnMag.Timer object is shown in Figure 3. In the get accessor, you check the number of passed arguments and raise a parameter count error if any are specified. Note that Figure 3 Adding Properties to MsdnMag.Timer function MsdnMag$Timer$get_Interval() { if (arguments.length !== 0) throw Error.parameterCount(); return this._interval; } function MsdnMag$Timer$set_Interval(value) { var e = Function._validateParams(arguments, [{name: ‘value’, type: Number}]); if (e) throw e; } this._interval = value; What if you want to add a public read/write property to a new object? In JavaScript you can’t play tricks to make an attribute with a get accessor or a set modifier usable via a property-based // Define the public interface of the class MsdnMag.Timer.prototype = { get_Interval : MsdnMag$Timer$get_Interval, set_Interval : MsdnMag$Timer$set_Interval, start : MsdnMag$Timer$start, stop : MsdnMag$Timer$stop, _callbackInternal : MsdnMag$Timer$_callbackInternal } Cutting Edge december2007 37
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.