O'Reilly logo

Prototype and Scriptaculous: Taking the Pain out of JavaScript by Chris Angus

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

2.1. Defining Classes

Since JavaScript is a prototype-based language rather than an object-oriented language, defining classes is a bit long winded.

Consider a simple Point class that has two attributes, x and y.

This may be defined in raw JavaScript as:

var Point = function(x,y)
{
       this.x = x;
       this.y = y;
}

function Point_toString()
{
       return "Point(" +this.x+","+this.y+")";
}

Point.prototype.toString = Point_toString;

New instances may be created using the new keyword:

var myPoint = new Point(0,0);

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required