please dont rip this site
Microsoft® JScript
Intrinsic Objects
| Tutorial |


Microsoft JScript provides a number of predefined objects. They are the Array, String, Math, and Date objects. You can think of these as "intrinsic" objects, to distinguish them from other objects that are part of the language. The intrinsic objects have associated methods and properties that are described in detail in the language reference.

The Array Object

In JScript, objects are handled as arrays and arrays are handled as objects. The subscripts of an array, which are entirely equivalent to the properties of an object, can be referred to by number (or by name, if you assign names to them). To create a new array, use the new statement 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 by using the Array keyword, JScript includes in the array a write-only length parameter, which records the number of entries in the array. 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, and the number of parameters is assigned to the length parameter, as in the following example, which is equivalent to the preceding one.


var theMonths = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
JScript automatically changes the value of length if you add elements to an array that you have created with the Array keyword.

The String Object

In JScript, strings are objects. This means that any time you declare a string variable or use a string literal, what you're actually doing is creating a new string object. 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. Under ordinary circumstances the first index is smaller than the second, and the substring method returns the characters starting at the first index and ending just before the second. If the first index is larger, the result is the same. If the two indices are equal, the method returns the empty string, "".


aString = "0123456789";
var aChunk = aString.substring(4, 7);  // Sets aChunk to "456".
var aNotherChunk = aString.substring(7, 4);  //  Sets aNotherChunk to "456".

// Using the preceding Array creation example:
firstLetter = theMonths [5].substring(0,1);  //  Sets the firstLetter variable to "J".
A property of the String object is the length property. This property contains the number of characters in the string, which is 0 for the empty string. This a numeric value, and can be used directly in calculations.

var howLong = "Hello World".length  //  Sets the howLong variable to 11.

The Math Object

The Math object has a number of predefined properties and methods. The properties are specific numbers. One of these is the value of pi (approximately 3.14159265358979...). This is the Math.PI property, shown in the following example.


//  JScript statements in which a radius variable is declared and assigned a numeric value.
var circleArea = Math.PI * radius * radius;  //  Capitalization of Math and of PI must be correct.
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 makes use of both pi and exponentiation.


volume = (4/3)*(Math.PI*Math.pow(radius,3));  //  Calculates the volume of a sphere with the given radius

The Date Object

The Date object lets you capture today's date, and also lets you calculate differences between dates. It has a number of properties and methods. 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. GMT stands for "Greenwich Mean Time,"; the preferred term is UTC, or "Coordinated Universal Time," which refers to signals issued by the World Time Standard.

NOTE: As far as JScript is concerned, time begins at midnight on January 1, 1970; you cannot ask JScript to create a Date object that represents an earlier time than that. If you need to deal with earlier times you must write your own code to do so, a formidable task.

The Date object has methods for converting GMT or UTC time to times in other time zones, and vice versa. Other methods let you get or set the day of the week, the date during the month, the month, the year, and the time.

To create a new Date object you use the new statement. The following example calculates, for the current year, the number of days that have passed and the number of days that are left.


var thisIsToday = new Date();  //  Assigns today's date, in "Day Month Date 00:00:00 Year" format,
//  to the thisIsToday variable.

var monthName = new Array(12)  {
monthName[0] = "Jan";
monthName[1] = "Feb";
monthName[2] = "Mar";
monthName[3] = "Apr";
monthName[4] = "May";
monthName[5] = "Jun";
monthName[6] = "Jul";
monthName[7] = "Aug";
monthName[8] = "Sep";
monthName[9] = "Oct";
monthName[10] = "Nov";
monthName[11] = "Dec";
}

var toDay = new Date();  //  Capture today's date.
var thisYear = toDay.getYear() + 1900;  //  Extract the year,
var thisMonth = monthName[toDay.getMonth()];//  ...the month,
var thisDay = thisMonth  + " " + toDay.getDate() + "," + (parseInt(toDay.getYear()) + 1900);  //  ...and the day.
thisDay = Math.round(Date.parse(thisDay)/8.64e7);  //  Determine the # of days since the start.

var firstDay = "Jan 1, " + thisYear;
firstDay = Math.floor(Date.parse(firstDay)/8.64e7);  //  Do the same for the beginning of the year...

var lastDay = "Dec 31, " + thisYear;
lastDay = Math.floor(Date.parse(lastDay)/8.64e7);  //  ...and the end of the year, in case it's a leap year.
var daysInYear = (lastDay - firstDay) + 1;  //  Compute the number of days in the year.

var daysElapsed = thisDay - firstDay;  //  Determine how many days have elapsed,
var daysLeft = daysInYear - daysElapsed;  //  ...and how many are left.

var comment1 = daysElapsed+ " days have elapsed in the year.";  //  Set up comments for most of the year.
var comment2 = "That means there are " + daysLeft + " days left in " + thisYear + ".";

if (daysElapsed == 0)  {  //  Cover the special cases: beginning & end of year, and single day.
comment1 = "It's January first, " + thisYear + ".";
}
if (daysElapsed == 1) {
comment1 = "Only one day gone so far.";
}
if(daysElapsed == daysInYear) {
comment1 = thisYear + " is just about over.";
}

if (daysLeft == 0)  {
comment2 = "Best wishes for the New Year!";
}
if (daysLeft == 1)  {
comment2 = "There's only one day left in " + thisYear + ".";
}
if (daysLeft == daysInYear)  {
comment2 = "Happy New Year!";
}

© 1996 by Microsoft Corporation.


file: /Techref/language/asp/js/302.htm, 8KB, , updated: 1996/11/22 10:12, local time: 2024/12/27 07:25,
TOP NEW HELP FIND: 
3.145.81.47:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://techref.massmind.org/techref/language/asp/js/302.htm"> Microsoft&#174; JScript Language Tutorial </A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to massmind.org!

 

Welcome to techref.massmind.org!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .