Object-Oriented JavaScript

CSE 190 M (Web Programming) Spring 2008

University of Washington

Except where otherwise noted, the contents of this presentation are © Copyright 2008 Marty Stepp, Jessica Miller, and Jim George, and are licensed under the Creative Commons Attribution 2.5 License.

Valid XHTML 1.1 Valid CSS!

Lecture outline

Why use classes and objects?

Interacting with objects

You have already used many types of JavaScript objects:

Creating a new anonymous object

var name = {
	fieldName: value,
	...
	fieldName: value
};
var pt = {
	x: 4,
	y: 3
};
alert(pt.x + ", " + pt.y);

You've already done this...

new Ajax.Request(
	"http://example.com/app.php",
	{
		method: "get",
		onSuccess: ajaxSuccess
	}
);

new Effect.Opacity("my_element",
	{
		duration: 2.0, 
		from: 1.0,
		to: 0.5
	}
);

Objects with behavior

var name = {
	...
	methodName: function(parameters) {
		statements;
	},
	...
};
var pt = {
	x: 4,
	y: 3,
	distanceFromOrigin: function() {
		return this.x * this.x + this.y * this.y;
	}
};
alert(pt.distanceFromOrigin());   // 5

A paradigm shift: prototypes

What if we want to create an entire new class, not just one new object?
(so that we could say new Point())

Using prototypes

Syntax for prototypes

// constructor
function className(parameters) {
	this.fieldName = value;
	...
	this.fieldName = value;
}

// adding a method to the prototype
className.prototype.methodName = function(parameters) {
	statements;
}

Prototype example

// Constructs a new Point object at the given initial coordinates.
function Point(initialX, initialY) {
	this.x = initialX;
	this.y = initialY;
}

// Computes the distance between this Point and the given Point p.	
Point.prototype.distance = function(p) {
	var dx = this.x - p.x;
	var dy = this.y - p.y;
	return Math.sqrt(dx * dx + dy * dy);
};

// Returns a text representation of this Point object.	
Point.prototype.toString = function() {
	return "(" + this.x + ", " + this.y + ")";
};

Practice problem: Favorite Movies

Creating classes

How Prototype (uppercase P) adds class semantics to JavaScript

Classes and prototypes

Creating a class

className = Class.create({
	// constructor
	initialize : function(parameters) {
		this.fieldName = value;
		...
	},

	functionName : function(parameters) {	
		statements;
	},
	...
});

Class.create example

Point = Class.create({
	// Constructs a new Point object at the given initial coordinates.
	initialize: function(initialX, initialY) {
		this.x = initialX;
		this.y = initialY;
	},

	// Computes the distance between this Point and the given Point p.	
	distance: function(p) {
		var dx = this.x - p.x;
		var dy = this.y - p.y;
		return Math.sqrt(dx * dx + dy * dy);
	},

	// Returns a text representation of this Point object.	
	toString: function() {
		return "(" + this.x + ", " + this.y + ")";
	}
});

Inheritance

className = Class.create(superclass, {
	...
});
// Points that use "Manhattan" (non-diagonal) distances.
ManhattanPoint = Class.create(Point, {
	// Computes the Manhattan distance between this Point and p.
	// Overrides the distance method from Point.
	distance: function(p) {
		var dx = Math.abs(this.x - p.x);
		var dy = Math.abs(this.y - p.y);
		return dx + dy;
	},
	
	// Computes this point's Manhattan Distance from the origin.
	distanceFromOrigin: function() {
		return this.x + this.y;
	}
};

Referring to superclass: $super

name: function($super, parameters) {
	statements;
}
ManhattanPoint3D = Class.create(ManhattanPoint, {
	initialize: function($super, initialX, initialY, initialZ) {
		$super(initialX, initialY);   // call Point constructor
		this.z = initialZ;
	},
	
	// Returns 3D "Manhattan Distance" from p.
	distance: function($super, p) {
		var dz = Math.abs(this.z - p.z);
		return $super(p) + dz;
	},

	// Overrides Point's toString method.	
	toString: function() {
		return "(" + this.x + ", " + this.y + ", " + this.z + ")";
	}
};

Practice problem: Fancy movies

Modify the movie gallery code from the last practice problem so that it uses Prototype's Class.create method to make the Movie type.

Once this is done, add a new subtype FancyMovie that displays its movie ratings as star images rather than just showing a number on the page.