Objects

An object is a set of values and methods (which are actually a special kind of function) used to deal with these values. Array is an example of an object, with its elements as a set of values and predefined functions like sort(), which can do something with its elements. Array also has a useful property, length, which can tell us a number of elements in our array.

But there are more important built-in objects in JavaScript. First of all, strings are treated as objects in JavaScript. A String object has the length property and a number of useful methods. Try the following code to see some of them in action:

var aString = "hi there!"
document.write(aString.length + " " + aString.toUpperCase() +
       "<br/>" + aString.bold() + "<br/>" + aString.blink() +
       "<br/>" + aString.toUpperCase().sub());

Date is a very useful object which allows you to easily display or modify a date. Try the following code:

// Creates a new Date object
// with the current date and time already in it
var aDate = new Date()

// Displays the date in the default format
document.write(aDate + "<br/>");

// Returns the number of a week day (0 for Sunday etc.)
document.write(aDate.getDay() + " ");

// Return the current hour, minute and second
document.write(aDate.getHours() + " ");
document.write(aDate.getMinutes() + " ");
document.write(aDate.getSeconds() + "<br/>");
// You can set some other date
aDate.setFullYear(2007, 6, 12)
document.write(aDate + "<br/>");

And, here is the output which I just had (yours will be different, of course):

Sun Feb 26 2006 13:12:03 GMT+0000 (GMT Standard Time)
0 13 12 3
Thu Jul 12 2007 13:12:03 GMT+0100 (GMT Daylight Time)

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset