Intrinsic Objects (Windows Scripting - JScript)
Microsoft JScript provides intrinsic (or built-in) objects. They are the Array, Boolean, Date, Error, Function, Global, JSON, Math, Number, Object, RegExp, Regular Expression, and String objects. The intrinsic objects have associated methods and properties that are described in detail in the language reference.
Array Object
The subscripts of an array can be thought of as properties of an object, and are referred to by their numeric index. Note that named properties added to an array cannot be indexed by number; they are separate from the array elements.
To create a new array, use the new operator and the Array() constructor, as in the following example.
var theMonths = new Array(12);
theMonths[0] = "Jan";
theMonths[1] = "Feb";
theMonths[2] = "Mar";
theMonths[3] = "Apr";
theMonths[4] = "May";
theMonths[5] = "Jun";
theMonths[6] = "Jul";
theMonths[7] = "Aug";
theMonths[8] = "Sep";
theMonths[9] = "Oct";
theMonths[10] = "Nov";
theMonths[11] = "Dec";
When you create an array using the Array keyword, JScript includes a length property, which records the number of entries. If you do not specify a number, the length is set to 0, and the array has no entries. If you specify a number, the length is set to that number. If you specify more than one parameter, the parameters are used as entries in the array. In addition, the number of parameters is assigned to the length property, as in the following example, which is equivalent to the preceding example.
var theMonths = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
JScript automatically changes the value of length when you add elements to an array that you created with the Array keyword. Array indices in JScript always start at 0, not 1, so the length property is always one greater than the largest index in the array.
String Object
In JScript, you can treat strings (and numbers) as if they were objects. The string Object has certain built-in methods, which you can use with your strings. One of these is the substring Method, which returns part of the string. It takes two numbers as its arguments.
var aString = "0123456789";
// This code sets aChunk to "456".
var aChunk = aString.substring(4, 7);
// This code sets anotherChunk to "456".
var anotherChunk = aString.substring(7, 4);
// This code sets the firstLetter variable to "J"
// by using the array in the preceding array creation example.
firstLetter = theMonths[5].substring(0,1);
Another property of the String object is the length property. This property contains the number of characters in the string (0 for an empty string). This a numeric value, and can be used directly in calculations.
var howLong = "Hello World".length // Sets the howLong variable to 11.
Math Object
The Math object has a number of predefined properties and methods. The properties are specific numbers. One of these specific numbers is the value of pi (approximately 3.14159...). This is the Math.PI property, shown in the following example.
var radius = 5;
var circleArea = Math.PI * radius * radius; // Note capitalization of Math and PI.
One of the built-in methods of the Math object is the exponentiation method, or pow, which raises a number to a specified power. The following example uses both pi and exponentiation.
// This formula calculates the volume of a sphere with the given radius.
volume = (4/3)*(Math.PI*Math.pow(radius,3));
Date Object
The Date object can be used to represent arbitrary dates and times, to get the current system date, and to calculate differences between dates. It has several properties and methods, all predefined. In general, the Date object provides the day of the week; the month, day, and year; and the time in hours, minutes, and seconds. This information is based on the number of milliseconds since January 1, 1970, 00:00:00.000 GMT, which is Greenwich Mean Time (the preferred term is UTC, or "Universal Coordinated Time," which refers to signals issued by the World Time Standard). JScript can handle dates that are in the approximate range 250,000 B.C. to 255,000 A.D.
To create a new Date object, use the new operator, as shown in the following example.
/*
This example uses the array of month names defined previously.
The first statement assigns today's date, in "Day Month Date 00:00:00 Year"
format, to the toDay variable.
*/
// Set variable to today's date.
var toDay = new Date();
// Extract the year, the month, and the day.
var thisYear = toDay.getFullYear();
var thisMonth = theMonths[toDay.getMonth()];
var thisDay = thisMonth + " " + toDay.getDate() + ", " + thisYear;
Number Object
In addition to the special numeric properties (PI, for example) that are available in the Math object, several other properties are available in Microsoft JScript through the Number object.
Property |
Description |
---|---|
MAX_VALUE |
Largest possible number, about 1.79E+308; can be positive or negative. (Value varies slightly from system to system.) |
MIN_VALUE |
Smallest possible number, about 5.00E-324; can be positive or negative. (Value varies slightly from system to system.) |
NaN |
Special nonnumeric value, "not a number." |
POSITIVE_INFINITY |
Any positive value larger than the largest positive number (Number.MAX_VALUE) is automatically converted to this value; represented as infinity. |
NEGATIVE_INFINITY |
Any value more negative than the largest negative number (-Number.MAX_VALUE) is automatically converted to this value; represented as -infinity. |
Number.NaN is a special property that is defined as "not a number." An attempt to parse a string that cannot be parsed as a number returns Number.NaN. NaN compares unequal to any number and to itself. To test for a NaN result, do not compare against Number.NaN; use the isNaN() function instead.
JSON Object
JavaScript Object Notation (JSON) is a data-interchange format based on a subset of the object literal notation of the JavaScript language.
The JSON object provides two methods to convert to and from JSON text format. The JSON.stringify method serializes objects and arrays into JSON text. The JSON.parse method de-serializes JSON text to produce in-memory objects. For more information, see An Introduction to JavaScript Object Notation (JSON) in JavaScript and .NET.
Change History
Date |
History |
Reason |
---|---|---|
September 2010 |
Modified information about the Number object. |
Content bug fix. |
March 2009 |
Modified examples. |
Content bug fix. |