JScript internal objects

Source: Internet
Author: User


Microsoft JScript provides 11 internal (or "built-in") objects. They are array, Boolean, date, function, global, math, number, object, Regexp, error, and string objects. Each object is related

Methods and attributes, which are described in the language reference. This section also describes some objects.

Array object
The array subscript can be considered an object attribute, which is referenced by a digital index. Note: The named attributes added to the array cannot be indexed by numbers. They are separated from array elements.

Use the new operator and the Array () constructor to generate a new array, as shown 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 an array is generated with the keyword array, JScript contains the Length attribute, which records the number of entries in the array. If no value is specified for this attribute, the length is set to 0 and the array has no entry point. If a value is specified, the length is set to this

Number. If more than one parameter is specified, these parameters are used as the entry to the array. In addition, the number of parameters is assigned to the Length attribute. The following example is equivalent to the previous one.

VaR themonths = new array ("Jan", "FEB", "Mar", "APR", "may", "Jun ",
"Jul", "Aug", "Sep", "Oct", "Nov", "dec ");
When an element is added to an array generated by the keyword array, JScript automatically changes the attribute length value. The array index in JScript always starts with 0 instead of 1, so the attribute length is usually 1 larger than the maximum index of the array.

String object
In JScript, strings (and numbers) can be processed as objects. The string object has some built-in methods that can be used with its own strings. One of them is the substring method, which returns part of the string. This method takes two numbers

As a parameter.

Astring = "0123456789 ";
VaR achunk = astring. substring (4, 7); // set achunk to "456 ".
VaR anotherchunk = astring. substring (7, 4); // set anotherchunk to "456 ".
// Use the preceding example to generate an array:
Firstletter = themonths [5]. substring (0, 1); // set the variable firstletter to "J ".
Another attribute of the string object is the Length attribute. This attribute contains the characters in the string (the Null String is 0 ). It is a numerical value and can be used directly in calculation.

VaR Howlong = "Hello world". Length // set the variable Howlong to 11.
Math object
Math objects have many predefined attributes and methods. Attribute is a special number. One of these special numbers is the PI value (approximately 3.14159 ...). This is the math. Pi attribute, as shown in the following example.

// Declare a radius variable and assign a value.
VaR circlearea = math. Pi * radius; // note that math and PI are capitalized.
A built-in method of the math object is the power multiplication (or POW) method, which is used to obtain the power of a specified number. The following example uses both Pi and power.

// This formula calculates the volume of the sphere with a given radius.
Volume = (4/3) * (math. Pi * Math. Pow (radius, 3 ));
Date object
The date object can be used to represent any date and time, obtain the current system date, and calculate the interval between the two dates. It has some predefined attributes and methods. Generally, the date object provides the week, month, day, and year, and the hour, minute

And the time in seconds. This information is based on January 1, 1970 00:00:00. the number of milliseconds starting with 000 GMT, in which GMT is the Greenwich Mean Time (the preferred term is UTC, or "global standard time", and the signal referenced by it is the "World Time"

Standard ). JScript can process dates in the range of 250,000 B .C. To 255,000 A.D.

Use the new operator to create a new date object. The following example calculates the past days of the current year and the remaining days of the current year.

/*
In this example, the array of month names is used.
The first statement is in the format of "day month date 00:00:00 year ".
Assign values to the thisistoday variable.
*/
VaR thisistoday = new date ();

VaR today = new date (); // obtain the date of today.

// Extract year, month, and day.
VaR Thisyear = today. getfullyear ();
VaR thismonth = themonths [today. getmonth ()];
VaR ThisDay = thismonth + "" + today. getdate () + "," + Thisyear;
Number object
In addition to the special numeric attributes (such as pi) available in the math object, the number object has several other numeric attributes in Microsoft JScript.

Attribute description
Max_value may have a maximum of about 1.79e + 308. It can be a positive or negative number. (The specific values vary slightly with the system .)
The possible minimum number of min_value values is approximately 2.22e-308; it can be positive or negative. (The specific values vary slightly with the system .)
Nan special non-quantity value, "not a value ".
Positive_infinity is automatically converted to any positive value greater than the maximum positive number (number. max_value), representing positive infinity.
Any value smaller than the smallest negative number (negative number. max_value) in negative_infinity is automatically converted to this value, representing negative infinity.

Number. Nan is a special attribute and is defined as "not a value ". For example, if it is divided by 0, Nan is returned. Trying to parse a string that cannot be parsed as A number also returns number. Nan. The result of comparing nan with any number or itself is not

Equal. The result cannot be tested by comparing it with number. Nan, but the isnan () function should be used.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.