Objects

Variables can be either primitive or objects. What this entails is the concept of object oriented programming. A variable allows the programmer to temporarily store data for future use in the program. A primitive variable allows the programmer to manipulate data using a single storage 'box' of the programmer's creation. An object allows the programmer to group the data as characteristics of an object, either one already in existence or one created by the programmer.

There are four types of objects:

A user-defined object is created much like any other variable, using the var statement. To define it as an object, the programmer adds the "= new Object()" constructor function.

var pizza = new Object();

The 'pizza' object can now be used to write a program dealing with various charactistics (variables) pertaining to 'pizza'.

pizza.type
pizza.price
pizza.whatever

Objects can even have levels of existence, with a property/characteristic being itself an object and forming an heirarchy.

var person=new Object();
person.name = new Object();
person.address = new Object();

Where:
person.age = 30;
person.name.first = "John";
person.name.last = "Doe";
person.address.street = "123 Center St.";
person.address.city = "Portsmouth";
person.address.state = "Ohio";
etc.

A JavaScript program can be written with all programmer-created variables being primitives. However, the programmer still has to work with objects. Because JavaScript is object-oriented. Remember document.write? It is part of the window object in JavaScript. This is an example of a Browser object. A browser object is not necessarily a part of JavaScript but most browsers support a linkage between JavaScript and the object to allow manipulation of the page.

Built-in objects such as Math, Date, Object, and Array are those associated by JavaScript wioth operations and manipulation of data.

Document objects are accessed using the document sub-object of the Window object. These objects work with the W3C's Document Object Model (DOM) to provide a structured interface with HTML and XML documents and allow manipulation of Cascading Style Sheets. This feature helps with pages utilizing dynamic HTML.

Objects have properties and methods. Properties are a value that can be accessed or changed, like any primitive variable. Properties are accessed by listing the object name followed by a period and the property name. Methods are an action or operation that can be initiated. Using them allows the programmer to control how a web page can be changed and/or the operation of the browser. Methods are accessed similar to properties, but also have trailing parenthesis immediately following the method name. Methods do not have to have all levels of the object heirarchy written to be invoked. Functions are special JavaScript objects that contain executable code, and are created using the function keyword.



Window Object
Properties

closed = y/n window closed
document = sub-object of window
history = sub-object of window
location=URL in/to be displayed
name=name of displayed page
Methods

alert(str) = displays alert window
close() = close current window
confirm(str)=displays confirm window
open(URL,name,"options")=opens new window
prompt(str,str)=displays prompt window
Example Properties

if(cont){window.location="nextpage.html";}
Example Methods

window.alert("Enter number between 0-10");
close();
confirm("Do you wish to continue?");
open("http://www.shawnee.edu","cpage","width=500,height=400");
prompt("Enter your name","");


Document Object
Properties

alinkColor=color of active links
bgColor=color of background
fgColor=color of text
Form=sub-object of document
Image=sub-object of document
lastModified=contains date document last changed (unreliable)
linkColor=color of unvisited links
referrer=URL that linked to page presently displayed (prev page)
Methods

write(str)=writes string to web page
Example Properties

document.alinkColor="00ff00";
document.bgColor="00000";
ldate=document.lastModified; (see Methods e.g.)
prevPage=document.referrer;
Example Methods

document.write(ldate);


History Object
Properties

length=how many documents since cleared
Methods

back()=same as back button on browser
forward()=same as forward button on browser
go(-num)=cause browser jump back indicated number of addresses
Example Properties

pviews=history.length;
Example Methods

window.history.back();
window.history.forward();
window.history.go(-1);



Date Object
No Properties for Date Object
Methods

getDate()=day of month (1-31)
getDay()=day of week(1-7)
getHours()=hours(0-23)
getMinutes()=minutes(0-59)
getMonth()=month(1-12)
getSeconds()=seconds(0-59)
getYear()=year(format:"2005")
Example of Use

(to print out Mon/Day/Year):
var cal=new Date();
document.write(cal.getMonth());
document.write("/");
document.write(cal.getDate());
document.write("/");
document.write(cal.getYear());



Math Object
Properties

E=Natural log base;
PI = value of Pi

These to 16 places of accuracy
Methods

abs(num)=absolute value of value in "num"
acos(num)=arc cosine of num in radians
asin(num)=arc sine of num in radians
atan(num)=arc tangent in radians
ceil(num)=least integer greater than num
cos(num)= cosine in radians
floor(num)=greatest integer less than num
log(num)=log base e
max(n1,n2)=returns whichever is greater of n1 and n2
min(n1,n2)=returns whichever is lesser of n1 and n2
pow(n1,n2)=value of n1 raised to n2 power
random()=generates random number between 0 and 1
round(num)=rounds to nearest integer
sin(num)=sine in radians
sqrt(num)=square root
tan(num)=tangent in radians
Example Properties

carea=r*r*Math.PI;
Example Methods

lgst=max(n1,n2);
p=pow(n1,n2); where value in n1 raised to n2 power
q=random(); puts random decimal number in q
amt=round(amt);
val=sin(n3);
res=sqrt(ab);
tan(num)=tangent in radians



Global Object
No Properties for Global Object
Methods

isNaN(num)=y/n is value a number
parseFloat(str)=convert string to floating decimal number
parseInt(str,num)=string->integer,
number specifies what number base system(optional)
Example Methods

if(isNaN(x)) { }
x=parseFloat(x);
x=parseInt(x);
x=parseFloat(x,2) binary


Lesson 4 of JavaScript

Return to main page