Sententiae viri ex temporibus duobus

Read Zackas Object Oriented Programming in JavaScript

I recently read Nicholas Zackas' The Principles of Object Oriented Javascript and I really recommend it. Many people I know (and students I teach) struggle with mapping the concept of object orientation onto a language that does not follow classical inheritance and lacks many of the visible signals that developers recognize as OO-ish.

The books makes forays into hidden attributes of the language but remains friendly throughout. It offers practical tools and exploratory exercises that will help JavaScript programmers learn more about this interesting and protean language.

I wrote the following precis of my take-aways:

  1. Introduction: OO Languages
  2. Encapsulation
  3. Aggregation (an object can reference another)
  4. Inheritance
  5. Polymorphism
  6. Javascript has these, but they are implemented in a way that appears different
  7. Primitive and Reference Types
  8. Primitive types: simple data
  9. Reference types: stored as objects
  10. Autoboxing casts primitive types to reference types
  11. Primitive Types
    1. Boolean
    2. Number
    3. String
    4. Null: A primitive type with only one value, null
    5. Undefined: Primitive type with only value undefined, value assigned to uninitialized value
    6. A variable holding a primitive holds the value
    7. IDENTIFY WITH: typeof operator
    8. SCREWBALL: typeof null // => "object"
    9. Identify nullity by === null
    10. Primitives are autoboxed to provide them an OO interface, but they are not objects
  12. Reference Types
    1. Represent objects in JavaScript
    2. Reference values are instances of reference types
    3. An object is an unordered list of properties consisting of a name and a value
    4. When the value points to a function it is called a method
    5. Creating Objects
    6. Use new
    7. Assigned variable holds a pointer
    8. Destroying can be accomplished by assigning null and letting the GC clean
    9. Properties can be added or removed at any time.
  13. Instantiating Built-in Types
    1. Array, Date, Error, Function, Objecdt, RegExp
    2. Many of them have literal forms: Object, Array, RegExp
    3. Object literal does not actually fire new Object()
  14. Property Acess
    1. Dot notation
    2. Bracket-and-String
  15. Identifying Reference Types: instance instanceof Class
  16. Identifying Arrays Array.isArray()
  17. Primitives are wrapped (autoboxed)
  18. Functions
  19. Functions have an internal property named [[Call]]
  20. Presence of [[Call]] means typeof returns “function”
  21. Declarations v. Expressions
    1. Declaration: function foo()
    2. Expression: var x = function()
    3. Function declarations are hoisted and thus can be used before definition
  22. Functions can be used as values
  23. Parameters
    1. You can pass any number of parameters
    2. arguments object exists with all passed parameters
    3. arguments is not an Array
    4. Overloading function definitions is not possible
    5. this: The currrent execution context
    6. Can be changed by three means
      1. call
      2. apply
      3. bind
  24. Understanding Objects
  25. Defining Properties
    1. When a property is put on an object, [[Put]] is called
    2. Allocates the memory
    3. Specified initial value and attributes about the property
    4. This results in an own property
    5. When a property is updated on an object, [[Set]] is called
    6. Replaces the current value
    7. Detecting Properties
    8. Use the in operator: "name" in person1
    9. May want to test person1.hasOwnProperty("name") to preclude Prototype tampering
    10. Removing properties
    11. Don’t merely set to null
    12. DO use the delete operator
  26. Enumeration
    1. By default all properties you add are enumerable
    2. Properties have the internal [[Enumerable]] attributes set to true
    3. To get a list of propereties, use Object.keys(): only returns instance properties!
    4. Test whether a property is enumerable with propertyIsEnumerable()
  27. Types of properties
    1. Two types
    2. Data properties: contain a value
    3. Accessor properties: don’t contain a value, but a function that returns a value (i.e. a “getter”)
      1. Define getter with: get propertyName(){}
      2. Define setter with: set propertyName(){}
  28. Property Attributes
    1. Common
    2. [[Enumerable]]: Can be enumerated?
    3. [[Configurable]]: Can be changed?
    4. By default all properties are both
    5. Change with Object.defineProperty(objectOwner, propertyName, propertyDescriptorObject)
    6. Data Property Attributes
    7. To additional properties not found in property attributes
    8. [[Value]]: Holds the actual value
    9. [[Writable]]: Can be overwritten?
    10. Data Property Attributes
    11. [[Get]]: Holds the actual value
    12. [[Set]]: Can be overwritten?
    13. Multiple properties can be defined with Object.defineProperties()
  29. Preventing Object Motification
    1. Object.preventExtension() sets the [[Extensible]] flag to false
    2. Seal the object with Object.seal(): existing things can be changed
    3. Freeze the object with Object.freeze(): non-ext; unchangeable
  30. Constructors and Prototypes
  31. Constructor is a function used with new to create an Object
    1. Ensures siilarity of properties and methods
    2. new will invoke even with out ()
    3. instanceof can determine whether an instance is from a constructor
    4. When created with new a constructor property is set on the instance such that instance.constructor === ConstructorFunction is true
  32. Prototype
    1. in operator returns true for both proto properties and own properties
    2. An instance keeps track of its prototype with [[Prototype]]
    3. Read prototype with Object.getPrototypeOf()
    4. Test if something is prototype with object.isPrototypeOf(instance)
    5. Set prototype with Object.setPrototypeOf()
  33. Inheritance
  34. Prototypal Inheritance
  35. Instances inherit their prototype, the proto inherits from its prototype
  36. See Listing 1

Listing 1

function Animal(species) {
  this.species = species
} = function() {
  console.log("As a: " + this.species + ", I like to eat, yum yum");

function Person(name) {, "Homo Sapiens");
  this.__proto__ = new Animal = name;

var k = new Person("Chris");;