The ECMAScript (JavaScript) objects have been enriched with some new methods in order to make the programmer’s transition from using the .NET Framework’s classes to JavaScript’s objects much easier.
The Array class (Figure A-9) represents an extension of the built-in JavaScript Array
object. It provides reflection information about the type and the type name of an object and also additional static methods offered by the .NET environment for arrays.
All the methods in this class are static methods.
var a = ["1", "2", "3"]; var b = ["11", "12", "13"]; // Add an element to an array Array.add(a, "4"); Sys.Debug.trace(a); // Output: 1,2,3,4 // Add a range of elements to an array Array.addRange(a, ["5", "6"]); Sys.Debug.trace(a); // Output: 1,2,3,4,5,6 // Clone an array var c = Array.clone(a); Sys.Debug.trace(a); // Output: 1,2,3,4,5,6 // Clear an array Array.clear(c); Sys.Debug.trace(c); // Output: // Check to see if an array has an element Sys.Debug.trace(Array.contains(a,"3")); // Output: true // Dequeue the last element from an array Sys.Debug.trace(Array.dequeue(a)); // Output: 1 // Apply a function to all the elements of an array Array.forEach(a,function(element, i, array){ Sys.Debug.trace("Array[" + i + "]=" + element); }); // Output: Array[0]=2 Array[1]=3 Array[2]=4 Array[3]=5 Array[4]=6 // Search an element in an array Sys.Debug.trace(Array.indexOf(a,"5",2)); // Output: 3 // Insert an element in an array at a specified position Array.insert(a,3,"8"); Sys.Debug.trace(a); // Output: 2, 3, 4, 8, 5, 6 // Parse a string and return an array Sys.Debug.trace(Array.parse('["0","1","2"]')); // Output : 0, 1, 2 // Remove an element from an array Array.remove(a, "1"); Sys.Debug.trace(a); // Output : 2, 3, 4, 8, 5, 6 // Remove an element at a specified position from an array Array.removeAt(a,2); Sys.Debug.trace(a); // Output : 2, 3, 8, 5, 6
The Boolean
class (Figure A-10) represents an extension of the built-in JavaScript Boolean
object. It provides reflection information about the type and the type name of an object and also an additional method offered by the .NET environment.
var bool = Boolean.parse("true"); Sys.Debug.trace(bool); // Output: true
The Date
class (Figure A-11) represents an extension of the built-in JavaScript Date
object. It provides reflection information about the type and the type name of an object and also additional methods offered by the .NET environment for creating and formatting a date.
Date
and Number
JavaScript variables can be formatted to their corresponding locale.
Predefined string patterns |
Description |
---|---|
|
Short date pattern : |
|
Long date pattern : |
|
Short time pattern : |
|
Long time pattern : |
|
Full date time pattern : |
|
Month day pattern : |
|
Sortable date time pattern :
|
|
Year month pattern : |
The above table represents the predefined string patterns for the InvariantCulture
. As we can see each predefined pattern translates into a series of format specifier characters. Below we can find the complete list of format specifier characters:
Format specifier character |
Description |
---|---|
|
Full day name (Sunday, Monday, etc.) |
|
Abbreviated day name (Sun, Mon, etc.) |
|
Day of the month, 2 digits (00...31) |
|
Day of the month (0...31) |
|
Full month name (January, February, etc.) |
|
Abbreviated month name (Jan, Feb, etc.) |
|
Month of the year, 2 digits (01...12) |
|
Month of the year (1...12) |
|
Year, 4 digits (2006) |
|
Year, 2 digits (06, 00, 99) |
|
Year (6, 0, 99) |
|
Hour (1...12) |
|
Hour (0...23) |
|
Hour, 2 digits (00...23) |
|
Hour (0...23) |
|
Minutes, 2 digits (00...59) |
|
Minutes (0...59) |
Seconds, 2 digits (00...59) | |
|
Seconds (0...59) |
|
AM or PM |
|
A or P (short for AM and PM) |
|
Hundreds of milliseconds, 1 digit (0...9) |
|
Hundreds and tens of milliseconds, 2 digits (00...99) |
|
Milliseconds (000...999) |
|
Time zone offset in hours (+7, -2) |
|
Time zone offset in hours, 2 digits (+ 07, -02) |
|
Time zone offset in hours and minutes (+07:00, -02:00) |
Note
The string passed to the format()
or localeFormat()
methods should have a single character if it is a predefined pattern, or at least two characters if it has format specifier characters.
// Displays the current date using the InvariantCulture object Sys.Debug.trace ((new Date()).format("F")); // Output: Friday, May 18, 2007 4:58:36 PM // Displays the current date using the CurrentCulture object set for French Sys.Debug.trace((new Date()).localeFormat("F")); // Output: vendredi 18 mai 2007 17:03:47 // Parse a string date using two date formats from InvariantCulture object. The first format is skipped and the second one is choosed Sys.Debug.trace(Date.parseInvariant("05/17/2007","MM-dd-yyyy","MM/dd/ yyyy")); // Output: Thu May 17 00:00:00 UTC+0300 2007 // Parse a string date using the predefined short date format from French CurrentCulture object. The short date pattern is dd/MM/yyyy Sys.Debug.trace(Date.parseLocale("17/05/2007","d")); // Output: Thu May 17 00:00:00 UTC+0300 2007
The Error
extension class (Figure A-12) contains a series of static methods that return Error
objects.
Static method that creates and returns an Error
object.
message – the optional message to be displayed in the error.
errorInfo – a JSON object containing a collection of keys and their values.
The method creates and returns an Error
object. It can receive an optional message and JSON object containing an additional collection of keys and their values. This collection will be added to the error message where the keys will represent additional attributes and the values would represent their values. The message
parameter is mapped as the message
attribute of the Error
object.
var e = Error.create(displayMessage, { name: "Sys.ArgumentException", paramName: paramName });
The preceding example is extracted from the debug version of the library in the Error.argument()
method.
Following are few other examples for Error
:
// Create and throw and argument error var err = Error.argument("valueParameter", "Invalid parameter"); throw err; /* Output: Sys.ArgumentException: Invalid parameter Parameter name: valueParameter */ // Create and throw a null argument error var err = Error.argumentNull("nullParameter", "The parameter does not accept null values"); throw err; /* Output: Sys.ArgumentNullException: The parameter does not accept null values Parameter name: nullParameter */ // Create and throw an argument out of range error var err = Error.argumentOutOfRange("valueParameter", 11, "The parameter accepts values between 1 and 10"); throw err; /* Output: Sys.ArgumentOutOfRangeException: The parameter accepts values between 1 and 10 Parameter name: valueParameter Actual value was 11. */ // Create and throw an argument type error var err = Error.argumentType("valueParameter", Date, Number, "The parameter is not the expected type"); throw err; /* Output Sys.ArgumentTypeException: The parameter is not the expected type Parameter name: valueParameter */ // Create and throw an undefined argument error if(typeof(parameter) === 'undefined') { var err = Error.argumentUndefined("parameter", "The parameter type is undefined"); throw err; } //Output Sys.ArgumentUndefinedException: The parameter type is undefined Parameter name: parameter // Create and throw an invalid operation error var err= Error.invalidOperation("An invalid operation has been detected"); throw err; // Output: Sys.InvalidOperationException: An invalid operation has been detected function test() { // Create and throw a not implemented error var err= Error.notImplemented("Method not implemented"); throw err; } test(); // Output: Sys.NotImplementedException: Method not implemented function testParams() { if(arguments.length>0) { // Create and throw a parameter count error var err= Error.parameterCount("Too many parameters"); throw err; } } testParams("test"); // Output: Sys.ParameterCountException: Too many parameters var err = Error.format("Bad format specified "); throw err; // Output: Sys.FormatException: Bad format specified
This class represents an extension of the built-in JavaScript Number
object. It provides reflection information about the type and the type name of an object, and also additional methods offered by the .NET environment for creating and formatting a number.
The patterns for the Number
object are:
Format specifier character |
Description |
---|---|
|
Decimal format |
|
Currency format |
|
Number format |
|
Percentage |
Please check the link below for more info:
http://msdn2.microsoft.com/en-us/library/44e531aa-1383-48ad-887b-
fa15d81566c3.aspx
// This example shows how the same value is parsed using the
InvariantCulture and the French CurrentCulture.
var a = Number.parseInvariant("2.55");
var b = Number.parseLocale("2,55");
Sys.Debug.trace(a + b);
// Output: 5.1
This class represents an extension of the built-in JavaScript Object
object and it provides reflection information about the type and the type name of an object.
var array = ["1","2"];
Sys.Debug.trace (Object.getTypeName(array));
// Output: Array
This class represents an extension of the built-in JavaScript RegExp
object and it provides reflection information about the type and the type name of a regular expression.
Without adding any additional functions, it offers the same functionality that is provided by JavaScript by default.
This class represents an extension of the built-in JavaScript String
object. It provides reflection information about the type and the type name of an object and also additional methods offered by the .NET environment for arrays.
// Trim a string var str = " My text "; Sys.Debug.trace(str.trim()); // Output:My text // Left trim a string var str = " My text "; Sys.Debug.trace(str.trimStart()); // Output:My text // Right trim a string var str = " My text "; Sys.Debug.trace(str.trimEnd()); // Output: My text // Test to see if a string starts with another string var str = "test string"; Sys.Debug.trace(str.startsWith("tes")); // Output:true // Test to see if a string ends with another string var str = "test string"; Sys.Debug.trace(str.endsWith("tes")); // Output:false // Format a string based on the arguments using the InvariantCulture object Sys.Debug.trace(String.format("On {0:F} the market share is {1:p}",new Date(),23.24)); // Output:On Friday, 18 May 2007 20:35:33 the market share is 23.24 % // Format a string based on the arguments using the CurrentCulture object Sys.Debug.trace(String.localeFormat("On {0:F} the market share is {1:p}",new Date(),23.24)); // Output: On vendredi 18 mai 2007 20:35:33 the market share is 23,24 %