Lecture Notes
Tutorial 2 - VARIABLES, FUNCTIONS, OBJECTS, AND EVENTS
function WildAnimal() {
this.habitat = ""; // jungle, desert, ocean
this.diet = ""; // carnivore, vegetarian
}
WildAnimal.prototype = new Animal;
function FarmAnimal() {
this.food_group = ""; // meat, dairy, poultry
this.location=""; // barnyard, stables, field
}FarmAnimal.prototype = new Animal;
Objects instantiated from either WildAnimal or FarmAnimal will include the three properties from the Animal constructor function, along with the properties specific to each individual object.
// create FarmAnimal object
chicken = new FarmAnimal();// properties from Animal constructor
chicken.animal_type = "chicken";
chicken.animal_sound = "cluck";
chicken.animal_transport_mode = "walk/fly";// properties from FarmAnimal constructor
chicken.location = "barnyard";
chicken.food_group = "poultry";Object Methods
// Create and discard an initial
Circle object.
// This is a bug work around
for Navigator 3.
new Circle(0,0,0);
// Define a constant property
that will be shared
// by all circle objects.
Circle.prototype.color = "Blue";
// Define a method to compute
the circumference
function Circle_circumference()
{
return (2 * Math.PI *
this.r);
}
// Assign the function to a prototype
property
Circle.prototype.circumference
= Circle_circumference;
// Define a method using the
Function() constructor and assign
// it to a prototype property
all in one step.
Circle.prototype.area = new
Function("return Math.PI*this.r*this.r;");
// The Circle class is defined.
// Now we can create an instance
and invoke its methods.
var c = new Circle(0.0, 0,0,
1.0);
var a = c.area();
var p = c.circumference();
<IMG> Image abort, error, load
<AREA> Area mouseOver, mouseOut
<BODY>…</BODY> Document body blur, error, focus, load, unload
<FRAMESET>…</FRAMESET> Frame set blur, error, focus, load, unload
<FRAME>…</FRAME> Frame blur, focus
<FORM>…</FORM> Form submit, reset
<INPUT TYPE="text"> Text field blur, focus, change, select
<TEXTAREA>…</TEXTAREA> Text area blur, focus, change, select
<INPUT TYPE="submit"> Submit click
<INPUT TYPE="reset"> Reset click
<INPUT TYPE="radio"> Radio button click
<INPUT TYPE="checkbox"> Check box click
<SELECT>…</SELECT> Selection
blur, focus, change