February 15, 2001 - Class-Based vs. Prototype-Based Languages | WebReference

February 15, 2001 - Class-Based vs. Prototype-Based Languages

Yehuda Shiran February 15, 2001
Class-Based vs. Prototype-Based Languages
Tips: February 2001

Yehuda Shiran, Ph.D.
Doc JavaScript

JavaScript is an object-oriented language not unlike Java and C++. One of the differences between JavaScript and the rest of the gang is that it is a prototype-based language while the other are class-based. A class-based language is based on two fundamental entities: classes and instances.

A class is an abstract data type. It describes a family of objects that have the same set of methods and properties. It also defines these properties and methods. For example, the shape class could represent the set of all shapes in a drawing. An instance is the instantiation of a class. It is one of the members of the family that the class represents. The shape on the top left corner of the page could be an instance of the shape class, representing a single particular shape out of all shapes shown on a drawing.

JavaScript, being a prototype-based language, does not distinguish between classes and instances. It has only objects, which are similar to instances. An object is a real, particular entity. The shape on the bottom right corner of a drawing is an object. You can use an existing object as a template for creating another object. The second object shares the first object's properties and methods. Any object might be used as a prototype for another object. Any object can also specify its own properties and methods, adding or overriding its existing ones.