You are previewing JavaScript Object Programming.
O'Reilly logo
JavaScript Object Programming

Book Description

This brief book explains the advantages of the object model, inheritance, both classical and prototypical, and shows how these concepts can be implemented in JavaScript. It also shows how object programming (OP) opens a new world of design possibilities that go far beyond inheritance.

This book will help the intermediate JavaScript programmer learn to use both types of inheritance. For classical inheritance, it is accompanied by a substantial online system (a windowing UI library) that shows classical inheritance at its best. The same system shows how OP "capabilities" can eliminate much of the need for inheritance.

For experienced JavaScript programmers, this book shows why most of the old views of JavaScript's inheritance have not done it justice. JavaScript classes inherit from JavaScript's prototypes, a fact that makes JavaScript's prototypes, when used correctly, functional equivalents to C++ classes (not to prototypes in true prototypal languages, like Self).

JavaScript's object programming (not inheritance) is what separates it from classical OOP languages like C++ and Java. Most important, basing inheritance on JavaScript's prototypal chain is possible, but is not the best choice for prototypal inheritance or classical inheritance.

Table of Contents

  1. Cover
  2. Title
  3. Copyright
  4. Dedication
  5. Contents at a Glance
  6. Contents
  7. About the Author
  8. A Note for the Implementers
  9. Introduction
  10. Chapter 1 : Creating Objects
    1. Reasons for Objects
      1. Objects Do Methods
      2. Event-Driven Programming
      3. Taming Exponential Complexity
    2. Class-Based vs. Prototypal
      1. Simula
      2. Smalltalk
      3. C++ and Java
      4. Self and JavaScript
    3. Objects Up Close
      1. Data Properties
      2. Methods (Code Properties)
    4. Ex Nihilo Object Creation
      1. The Object Constructor
      2. Object Literals
      3. More Ex Nihilo Objects
    5. OOP-Style Object Creation
      1. Constructors
      2. Assigning Initial Property Values
      3. Creating Instance Methods
      4. Creating Class Statics
      5. Getters and Setters
      6. Default Values
    6. Prototypal Object Creation
      1. Object Prototypes
      2. The Prototype Chain
      3. Object Prototype Cloning
    7. Summary
  11. Chapter 2 : Object Programming
    1. JSWindows Sample System
    2. OP Removes Restrictions
    3. OP Defined
    4. Programming with Properties
      1. Dot Notation
      2. Subscript Notation
    5. Object Programming Examples
      1. Object Sum
      2. OP for Inheriting Prototypes
    6. OP in the JSWindows Library
      1. DOM Related
      2. Utility
    7. Summary
  12. Chapter 3 : Inheritance Theory
    1. Classes
      1. Constructors
      2. Instance Methods
      3. Class (Family-Wide) Properties
    2. Class-Based Inheritance
      1. Property Sets
      2. Constructing an Extending Instance
      3. Overriding Properties
      4. Inheritance Chains
    3. Prototypal Inheritance
    4. Inheritance vs. Composition
      1. Composition in Theory
      2. Composition in JSWindows
    5. Summary
  13. Chapter 4 : Inheritance Practice
    1. Cascading init() Methods for Data
      1. A Theoretical Example
      2. A Practical Example
    2. Prototypes for Methods
      1. Prototype Inheritance Alternatives
      2. Prototype Alternatives
    3. JSWindows Inheritance
    4. Summary
  14. Chapter 5 : On OOP Principles
    1. Ranking OOP Principles
    2. Inheritance
    3. Encapsulation
      1. Access Specifiers
      2. Closures
    4. Polymorphism
      1. Subtype Polymorphism
      2. Parametric Polymorphism
      3. Ad Hoc and Other Polymorphism
      4. JavaScript and Polymorphism
    5. Classes, Abstraction, and Interfaces
      1. Classes
      2. Abstraction
      3. Interfaces
    6. Other OOP Principles
    7. Summary
  15. Chapter 6 : More Ex Nihilo Objects
    1. The Ex Nihilo Namespace Object
    2. The Ex Nihilo Class
      1. ReturningEx Nihilo Objects
      2. The Function as an Ex Nihilo Class
    3. Summary
  16. Chapter 7 : Inheritance Alternatives
    1. Multiple Inheritance
    2. Interfaces
    3. Capabilities
    4. The Window[_M[_BS]] Problem
    5. Mixins
    6. Calling Capability Methods
      1. Capabilities as Constructor Properties
      2. Capabilities as Single Properties
      3. Capability Prototype Methods
    7. Examples
      1. Closable
      2. Maskable
      3. Button_sizable
    8. Summary
  17. Chapter 8 : Designing for JavaScript
    1. Use Ex Nihilo Constantly
      1. Array Literals
      2. Styles Objects
      3. Other Objects
    2. Use Composition Liberally
      1. Mature Pos_size
    3. Use Capabilities Liberally
    4. Use Inheritance Conservatively
    5. Summary
  18. Chapter 9 : On Constructors
    1. Constructor Magic
      1. The new Operator
      2. The this Parameter
      3. The constructor.prototype
      4. The “[[prototype]]” Property
      5. The Prototype’s Prototype
      6. “[[prototype]]” Implies
    2. The Dynamic Prototype
    3. A Bit More Magic
      1. The Constructor Returns this
      2. The “Magic” Summarized
    4. Constructors Are Not for Inheritance
    5. Summary
  19. Chapter 10 : Appendices
    1. A Surveyed Pages, OOP Principles
    2. B Selected Books
      1. C++
      2. Java
      3. JavaScript
      4. Python
      5. Visual Basic
    3. C Selected Websites
      1. Wikipedia on Object-Oriented Programming, Class-Based Inheritance and Prototypal Inheritance
      2. The Author’s Web Site on Class-Based Inheritance and JavaScript Programming
      3. Other Web Sites on Class-Based Inheritance and Prototypal Inheritance
    4. D Defined Terms
    5. E Support for Selected Statements
    6. F Simple Closure
    7. G Sealing and Freezing Objects
    8. H Configuring Properties
    9. I Dynamic Properties and Me
  20. Index