retention and protection and hiding of state- process, and extreme late-binding of all things. It can be done in Smalltalk and in LISP. There are possibly other systems in which this is possible, but I'm not aware of them.” 1 ~ Dr. Alan Kay 1. In a 2003 letter to Stefan Ram
(procedural) • Object oriented • Functional It’s possible to program purely in one paradigm, but a hybrid style is more typical In the sense that everything except primitives is an object, JavaScript is more object- oriented than many mainstream OO languages
is minimalist: • Objects are simple dictionaries of key/value pairs • Properties and methods can be added and removed at runtime • Objects can directly manipulate each other’s state • Constructors are optional • Prototypes are optional
of objects • Constructors are optional, there is no default constructor • Constructors are just functions, with no special syntax or ceremony 1 • Must be invoked with “new” keyword to create objects 1. The JavaScript community has embraced the convention of capitalizing constructor names
has prototypes. 1 A prototype is just an object, to which behavior is delegated by another object • Prototypes are optional 2 • Changes to a prototype affect instances created from them 1. ECMAScript 2015 (ES6) introduces class syntax, but it is unrelated to class-based OOP 2. By default, objects have the built-in Object object as their prototype
a controversial feature. Classes are just syntactic sugar atop constructors and prototype chains Pros: • Syntax is initially friendlier to class-based OO developers • Standardization makes tooling simpler Cons: • Syntax is very different from underlying semantics • Mismatch confuses very same class-based OO developers
Seems crazy, but it’s what makes prototypal OO work, and it allows objects to “borrow” behavior from other objects. var obj1 = { name: 'foo', speak: () => 'My name is ' + this.name }; var obj2 = { name: 'bar' }; obj1.speak.call(obj2); // 'My name is bar'
is delegated up a prototype chain until an implementation is found. • If an object has the method, invoke it • If an object doesn’t have the method, check its prototype • If the object has a prototype, and the prototype has the method, invoke it • Rinse and repeat until the top of the chain (Object.prototype) • If the chain ends without an implementation, an exception occurs
or more source objects into a new destination object. • Dynamic extension of objects • Popularized by $.extend() and _.extend() • Supported natively with Object.assign() • Enables multiple inheritance
JavaScript is a weakly-typed, dynamic language - simply replaced in child objects. • Override a prototype method - analogous to class-based polymorphism • Replace an instance method - dynamic modification of objects • Extending a parent’s methods - ES5 syntax + “super”
signature since methods can accept a variable number of arguments, with no type-checking. var obj = { foo(arg1, arg2) { if (arguments.length === 2 && typeof arg1 === 'object') { // do behavior } else { // do other behavior } } };
home, you might keep it empty but you could live there. • A prototype is a working object, you can use it in your program • Metaphor breaks down when modifying a prototype A class is like a blueprint, it describes how a home is built but you can’t live there. • A class is a template for a working object, you can’t use it in your program 1. Metaphor borrowed from Reginald Braithwaite.
behavior to another object • The only linkage between the objects is the delegation pointer • An object uses delegate behaviors without owning/referencing them In class-based languages, inheritance is achieved by extending existing classes • Child object must have a memory or structural similarity to its parent • Child object owns/references inherited behaviors
late binding of behavior • Altering a prototype alters all existing instances created from it Most class-based languages enforce early binding of behavior • Once an instance is created, its behavior cannot be altered
• is a prototype-based, or prototypal, OO language • is not the only prototype-based language, but is by far the most mainstream • supports encapsulation, inheritance, and polymorphism • supports both prototypal and concatenative inheritance • differs considerably from class-based languages
Prototypes - Kyle Simpson Speaking JavaScript: Objects and Inheritance - Dr. Axel Rauschmayer Eloquent JavaScript: The Secret Life of Objects - Marijn Haverbeke