Page 160 - DCAP202_Fundamentals of Web Programming
P. 160
Unit 13: Building Object of JavaScript
myObj.getAge = Notes
function () {
return this.age;
};
document.write(myObj.name);
document.write(“<br/>”);
document.write(myObj.age);
document.write(“<br/>”);
document.write(myObj.getAge());
If the name of the property or method is not a simple identifier, or it is not known at the time
you write the script, you can use an expression inside square brackets to index the property.
!
Caution The names of all expando properties in JavaScript are converted to strings before
being added to the object.
In JavaScript, objects and arrays are handled almost identically, because arrays are merely a
special kind of Object. Both can have properties and methods.
Arrays have a length property, but objects do not. When you assign a value to an element of an
array whose index is greater than its length (for example, myArray[100] = “hello”), the length
property is automatically increased to the new length. Similarly, if you make the length property
smaller, any element whose index is outside the length of the array is deleted.
Example:
/ An array with three elements
var myArray = new Array(3);
// Add some data
myArray[0] = “Hello”;
myArray[1] = 42;
myArray[2] = new Date(2000, 1, 1);
document.write(“original length is: “ + myArray.length);
document.write(“<br/>”);
// Add some expando properties
myArray.expando = “JavaScript!”;
myArray[“another Expando”] = “Windows”;
// This will still display 3, since the two expando properties
// don’t affect the length.
document.write(“new length is : “ + myArray.length);
Self Assessment
Fill in the blanks:
1. JavaScript’s ................................... object provides advanced arithmetic and trigonometric
functions, expanding on JavaScript’s basic arithmetic operators.
LOVELY PROFESSIONAL UNIVERSITY 153