8.2. Client-Side Versions of .NET ClassesIn addition to adding OOP-like features for JavaScript coding, Atlas implements client classes that are analogs of some .NET classes. By doing this, two goals are achieved:
In my opinion, this is one of the areas where upcoming Atlas versions will most certainly add more features, so the following list of classes is neither exhaustive nor final. Two useful features that are already available are Sys.StringBuilder and enumerations. 8.2.1. Sys.StringBuilderOne of the new features introduced in .NET 1.0 that really paid off in terms of performance was the introduction of the StringBuilder class. The problem is that applications are usually full of code like this: string s = "", t; while () { t = <value>; s += t; } The problem lies in s += t, which is equivalent s = s + t. Whenever this code is executed, a copy of s and a copy of t is created in memory, then concatenated, and finally saved back into s. However copying s could be unnecessary. StringBuilder uses an optimized algorithm for string concatenation. In JavaScript, this approach does not have any measurable effect on memory (in fact, the implementation seems to be a tick slower than the standard approach). On the other hand, performance is not as large of an issue for client script as it is for server code. Nevertheless, for consistency with your server coding techniques, you can rely on your .NET coding techniques and use StringBuilder on the client-side. Example 8-7 puts the StringBuilder class to work. It concatenates some strings to build an HTML chessboard. Example 8-7. Using an Atlas StringBuilder
The built-in JavaScript function String.fromCharCode() converts an ASCII code to its associated character, so the inner for loop runs from "a" tHRough "h". As Figure 8-4 reveals, the code in Example 8-7 creates a simple chessboard. Figure 8-4. A chessboard (with some potential)8.2.2. EnumerationsAnother .NET type that is emulated by Atlas for JavaScript is the Enum type. This is implemented by the Atlas Web.Enum class. Unfortunately, it is impossible to use the JavaScript for...in syntax to iterate through a Web.Enum enumeration. A workaround is to create a custom enumeration using the Type.createEnum() method. As parameters, you first provide the name of the enumeration and then all values and indexes, in the following fashion: Type.createEnum( "ORA.MyEnums.Ajax", "Asynchronous", 0, "JavaScript", 1, "and", 2, "XML", 3); The getValues() method of the enumeration then returns all values of the enumeration (which are internally stored as an array), as shown in Example 8-8. Example 8-8. Using an Atlas Enum
This code outputs the string "Asynchronous JavaScript and XML " in the <div> element. The main use of the Atlas enumerations lies in these two helper functions:
Enumerations are also used internally by Atlas, for instance to create a set of browser types: Type.createEnum('Sys.HostType', 'Other', 0, 'InternetExplorer', 1, 'Firefox', 2); So as you can see, Atlas offers some interesting additional features for client scripting: new OOP constructs can help structuring complex code, and JavaScript versions of .NET classes add familiarity to the coding process. |