Some ECMAScript Predefined Objects
ECMAScript Basics - Part 12
Forward: In this part of the series, I introduce you to the String, Math and Date ECMAScript predefined objects.
By: Chrysanthus Date Published: 25 Jul 2012
Introduction
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
The String Object
Consider the statement:
var myStr = "I am a man";
“I am a man” is a string literal, meaning it is the actual string. You can create an object in which this would be the main property of the object. You create a string object using the String constructor, which is:
new String()
With this, the string literal is the argument of the string constructor. You can assign the created object to a variable. So you can have a statement like this:
var hisStr = new String("You are a woman.");
Try the following code:
<script type="text/ECMAScript">
var hisStr = new String("You are a woman.");
alert(hisStr);
</script>
When you create a string object using the String constructor and assign it to a variable, the value of that variable is the string literal. The variable is actually an object. It is a predefined object already having properties and methods, some of which we look at in this part of the series.
You can use the length property of the String object to know the number of characters (including the spaces) of a string. Try the following code:
<script type="text/ECMAScript">
var hisStr = new String("You are a woman");
alert(hisStr.length);
</script>
To access the length property you use the expression, objectName.length .
The charAt String Method
Character position number counting begins from zero. The position of a character in a string is called the Index. The charAt method takes the index as argument. The charAt method returns the character at the specified index. Try the following code:
<script type="text/ECMAScript">
var hisStr = new String("You are a woman");
alert(hisStr.charAt(2));
</script>
The indexOf String Method
This method returns the index within the calling String object of the start of a group of characters within the string literal. It returns -1 if the group is not found. Of course, the group can be made up of just one character. Try the following code:
<script type="text/ECMAScript">
var hisStr = new String("You are a woman");
alert(hisStr.indexOf('are'));
</script>
The Math Object
This is a predefined object that has properties and methods for mathematical constants and functions. You can automatically access it without creating an object. It has a good number of properties and methods. Well, we shall look only at its random and round methods.
The random Math Method
This method returns a pseudo-random number between 0 and 1. The number return is a proper fraction. You can multiply it by any whole number to have any bigger number you want. Read and try the following example:
<script type="text/ECMAScript">
var myRandom = Math.random()
alert(myRandom);
var myNum = myRandom * 50;
alert(myNum);
</script>
This method returns the value of a number rounded to the nearest integer. Read and try the following code:
<script type="text/ECMAScript">
var myRound = Math.round(25.85634);
alert(myRound);
</script>
The Date Object
In your program, you can use the Date object to work with dates and times. The Date object has a lot of methods. It does not really have properties.
ECMAScript stores dates (time included) as the number of milliseconds since January 1, 1970, 00:00:00 UTC (also known as Greenwich Mean Time (GMT)). After creating the date object you can get (read) the Month, day, year, hours, minutes and seconds. You can also set (change) these values. The date object is not necessarily your computer hardware date and time. The date object is an ECMAScript object whose date and time can be different from the computer hardware date and time. You can do calculations with the date object; for example you can have two date objects and find the difference between the two dates in milliseconds.
The syntax to create a date object is:
dateObjectName = new Date([parameters])
Whenever you see square brackets as with [parameters] in syntax, know that the component is optional. The dateObjectName is a variable. “new Date([parameters])” is the Date constructor.
Any of the followings can be the parameters.
- Nothing: If you do not put anything, then the syntax becomes:
dateObjectName = new Date()
- The parameters can be a string representing the a date in the form: "month day year hours:minutes:seconds", in that order. An example is:
myBirthDate = new Date("November 26 1995 13:30:00")
If you omit hours, minutes, or seconds, the value will be set to zero.
- The parameters can be a set of integers for year, month, and day, in that order. Example:
myBirthDate = new Date("1995, 10, 26")
Here, commas separate the arguments. You begin with the year figure, then month figure and then month day number.
Or the parameters can be a set of integers for year, month, day, hour, minute, and seconds, typed in that order, separated by commas. Example:
myBirthDate = new Date("1995, 10, 26, 14, 45, 0")
- One integer representing milliseconds since January 1, 1970, 00:00:00 UTC
These are the possible integers you can use:
Seconds and minutes: 0 to 59
Hours: 0 to 23
Day: 0 (Sunday) to 6 (Saturday)
Date: 1 to 31 (day of the month)
Months: 0 (January) to 11 (December)
Year: four digits since 1900
Some Date Object Methods
You can read (get) the components of the date with the following methods (as given in the ECMAScript Reference):
getDate : Returns the day of the month for the specified date according to local time.
getDay : Returns the day of the week for the specified date according to local time.
getFullYear : Returns the year of the specified date according to local time.
getHours : Returns the hour in the specified date according to local time.
getMilliseconds : Returns the milliseconds in the specified date according to local time.
getMinutes : Returns the minutes in the specified date according to local time.
getMonth : Returns the month in the specified date according to local time.
getSeconds : Returns the seconds in the specified date according to local time.
getTime : Returns the numeric value corresponding to the time for the specified date according to local time.
parse : Returns the number of milliseconds in a date string since January 1, 1970, 00:00:00, local time.
setDate : Sets the day of the month for a specified date according to local time.
setFullYear : Sets the full year for a specified date according to local time.
setHours : Sets the hours for a specified date according to local time.
setMilliseconds : Sets the milliseconds for a specified date according to local time.
setMinutes : Sets the minutes for a specified date according to local time.
setMonth : Sets the month for a specified date according to local time.
setSeconds : Sets the seconds for a specified date according to local time.
setTime : Sets the value of a Date object according to local time.
For the above methods, the integer values and not the string values are used. For example, 11 instead of November is used.
Note that there is no setDay method as the day of the week is set automatically when the year, month and month day number have been given.
Read and try the following code:
<script type="text/ECMAScript">
myBirthDate = new Date("1995, 10, 26");
myBirthDate.setFullYear(2000);
myBirthDate.setMonth(5);
myBirthDate.setDate(15);
alert(myBirthDate.getFullYear());
alert(myBirthDate.getMonth());
alert(myBirthDate.getDate());
alert(myBirthDate.getDay());
</script>
For each of the three objects given, I have not given all the properties and methods. ECMAScript has other predefined objects. You should consult some reference document for all the properties and all the methods and the other predefined objects.
Let us end here and continue in the next part of the series.
Chrys
Related Links
Major in Website DesignWeb Development Course
HTML Course
CSS Course
ECMAScript Course
NEXT