MSDN Magazine - December 2007 - (Page 36) Let’s take a closer look at this code fragment, as it contains some important clues about Microsoft AJAX Library programming. To start out, compare this code to some equivalent C# code: namespace MsdnMag { public class Timer { public Timer() { _interval = 5000; _timerID = null; } protected int _interval; protected object _timerID; public void start(Delegate callback) { // Any code that can create a timer _timerID = CreateTimer(callback, _interval); } } } The source code of the MicrosoftAJAX Library is located at ajax.asp.net/ downloads. You can modify these scripts in your own pages. A Class According to the Microsoft AJAX Library In traditional JavaScript programming, you work by creating functions. Because a function is a first-class element, you can create new instances of a function and configure it for execution. Put another way, you use functions much as if they were objects in an object-oriented language. This is a pillar of the JavaScript architecture and can’t be changed without the serious risk of breaking many of applications and a large number of existing browsers. The Microsoft AJAX Library performs some tricks and makes some assumptions with the final goal of making JavaScript functions look and behave like classic objects. You create Microsoft AJAX Library classes using the prototype model. All JavaScript functions feature the prototype property. The prototype property is meant to represent the internal-yet-public schema of the object. All instances of that function will end up calling the same method instance if the method is defined as part of the function’s prototype. As an example, suppose you have a Timer object: MsdnMag.Timer = new function() { // This is a sort of constructor // for the function : this._interval = 5000; } The first thing you should notice is that you have no need to explicitly define _timerID and _interval as part of the Timer object in the Microsoft AJAX Library. You just assign both members a default value in the constructor. However, to mark them as members of the object, you should prefix them with the “this” keyword. If you don’t, they would be considered private members of the function and not bound to the object’s prototype. When, prefixed with the “this” keyword, you can invoke those members from any prototyped methods. The MsdnMag.Timer function, as defined in this column, illustrates the recommended approach for building new classes on top of the Microsoft AJAX Library in ASP.NET AJAX and any other platform where the Microsoft AJAX Library is used. Take a look at Figure 1 and compare it to the preceding code snippets. Here, the JavaScript object is written as a closure rather than as a prototyped object. Both models are supported by the JavaScript engine supplied by most browsers. So what’s the difference? The closure model delivers an object that has all of its members entirely defined in the same overall context, much like a C# class. The prototype model serves up a class with a sort of distributed scheme. All members attached to the prototype of the object are shared by 36 msdnmagazine Cutting Edge http://ASP.NET http://ajax.asp.net/downloads http://ajax.asp.net/downloads http://ASP.NET
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.