In this book, you were first introduced to class diagrams in Chapter 5. There you learned that unlike in C#, several conventions need to be used in order to implement typical OOP concepts using JavaScript code. Class diagrams use different conventions for the different types of items they describe, such as class fields, properties, methods, and events.
Fields are variables of primitive types and can be accessed directly using a class instance. Properties are mechanisms where the values of class fields can be accessed or altered only through getter and setter functions. This restriction is by convention only; the fields that store property values can be accessed directly, but the convention requires using the getter and setter methods.
The property’s field that internally store its value is declared with a leading underscore (_
). The getter and setter methods are prefixed with “get_
” and “set_
” followed by the property name. For example, a property called name
would be implemented using JavaScript code like this:
this._name = myvalue; ... get_name: function() { return this._name; }, set_name: function(value) { . this._name = value; }
Methods are functions inside a class. The term comes from the OOP world, but in the case of JavaScript, method and function can often be used interchangeably.
Events represent notifications that an action has occurred. The implementation for events in JavaScript is very similar to the one chosen on the server-side .NET platform.
The JavaScript implementation for an event named change
looks like this:
this._events = new Sys.EventHandlerList(); ... // register a change event handler add_change: function(handler) { this.get_events().addHandler("change", handler); }, // unregister a change event handler remove_change: function(handler) { this.get_events().removeHandler("change", handler); }
When an event is raised, one or more functions, called handlers, can be invoked in response. Handlers can be registered and deregistered from an event. Each event handler would typically have a signature like the following:
function MyHandler(source, eventArgs)
Here, source
represents the object that raised the event, and eventArgs
contains an object derived from Sys.EventArgs
which contains the parameters sent to our event. This strongly resembles the .NET style of event handlers.
The diagrams in this book have been created using the Class Diagram feature of Visual Studio 2005 Team System Edition. Note, however, that this feature doesn’t actually work with JavaScript—to create class diagrams, we created C# classes that correspond to the Microsoft AJAX Library functions. The mentioned conventions were used to port the JavaScript code to C#. Here are a few examples:
Unlike public methods, private (by convention) JavaScript methods are named with a leading underscore. See Figure A-1.
JavaScript properties (by convention) are declared as C# properties (see Figure A-2); read-only JavaScript properties are represented as C# properties having only a getter (see Figure A-3).
Private (by convention) JavaScript fields are represented as C# fields with leading underscore(s)—see Figure A-4; all public Javascript fields are mapped to public C# fields (see Figure A-5).
All the events (by convention) exposed by JavaScript objects are mapped to C# events; the classic
EventHandler
in C# is replaced by convention byFunction
(see Figure A-6).