JavaScript Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 2.20

Prototype-Dased Object-Oriented Programming Style

This section provides a quick description of object-oriented programming and prototype-based programming style.

There are three primary goals of object-oriented programming:

  • Encapsulation - The ability to conceal functional details of an object from objects that send messages to it In other words, encapsulation is the ability to hide functional details of an object behind an interface - a collection of methods for other objects to call.
  • Inheritance - The ability for an object of a derived type to inherit attributes and behaviors from a parent type.
  • Polymorphism - The ability for two objects of a parent type and a derived type to respond to the same message from other objects with their own type specific behaviors.

These goals can be archived with two different programming styles:

  • Class-Based Programming - An object is created through an instantiation process based on the specified class, which is statically defined with the structure and behavior of the resulting object.
  • Prototype-Based Programming - An object is created through a cloning process based on another object serving as an prototype, which can be dynamically modified.

JavaScript is a prototype-based programming language which supports encapsulation, polymorphism, and inheritance as described below:

  • Encapsulation - Functional details of an object are hidden inside properties and methods of the object.
  • Inheritance - Properties and methods of a parent type are inherited through a prototype object.
  • Polymorphism - Inherited properties and methods can be overridden by an object of a derived type to respond the same interface specific to this object.

See other sections of this chapter for detailed descriptions and tutorial examples of JavaScript prototype-based programming features.

Table of Contents

 About This JavaScript Tutorial Example Book

 Introduction to JavaScript

 ECMAScript Language Specification and JavaScript Dialects

 Data Types, Variables and Expressions

 Flow Control Statements

 Creating, Accessing, and Manipulating Arrays

 Defining and Calling Functions

 Web Browser Supporting JavaScript

 Server-Side and Client-Side Web Scripting

 Introduction to Objects

Defining Your Own Object Types

Prototype-Dased Object-Oriented Programming Style

 Prototype-Based Programming Features in JavaScript

 Defining Object Constructor Functions

 Adding Properties and Methods to Objects

 "for ... in" and "for each ... in" Statements

 "prototype" Property of the Constructor Function Object

 "instanceof" Operator - Determining Object Type

 "typeof" Operator and Data Types

 Inheritance of Properties and Methods through the Prototype Object Chain

 'jrunscript' - JavaScript Shell Command from JDK

 Using Functions as "Function" Objects

 Introduction to Built-in Object Types

 W3C's Document Object Model (DOM) Specifications

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2008
Prototype-Dased Object-Oriented Programming Style