Rethinking twitter.com ‣ Use the right tool for the job. Don’t JavaScript all the things. ‣ Take advantage of the browser and of the nature of JavaScript rather than fighting it.
Prototypes ‣ JavaScript’s Agent of Re-use ‣ Flexible but Awkward ‣ new syntax encourages classical model ‣ Prototype chains are single rooted ‣ Inheritance syntax is gnarly
Class syntax ‣ JS native class syntax scheduled for ES6 ‣ class, extend and super are reserved words ‣ Library implementation varies widely and abstraction is often leaky (‘const’, ‘static’) ‣ In particular, the relationship between this and super is difficult
Classical inheritance ‣ Requires up-front knowledge of general case ‣ Single rooted hierarchy ‣ Types are often too generalized to map to real life coding problems (some animals swim AND walk)
Beyond classification ‣ 19th century British philosophers Whewell and Jevons argued that there are no objectively “right” classifications ‣ “In general, when working with prototypes, one typically chooses not to categorize but to exploit alikeness.” Antero Taivalsaari (Nokia Research Center) - Journal of Object Oriented Programming Nov/Dec 1997
Property copy mixins ‣ No hierarchical constraints ‣ Functionality is grouped by what it does not who it belongs to ‣ BUT... ‣ Mixin has no reference to target properties ‣ Properties can only clobber ‣ Target object requires intimate knowledge of mixin properties
Functional mixins ‣ Mixins are functions that assign properties to the ‘this’ object. ‣ Mixins are directly invoked in the context of the target object by means of call/apply
Advice: before, after and around ‣ A mixin that adds, underscore.js style, before(), after() and around() to an object. ‣ Other mixins can use this to augment existing methods.
Functional mixins ‣ Mixins as verbs instead of nouns. ‣ Mixins are functions. We can take advantage of closure scope, arguments and context. ‣ A mixin can be applied to any object type: prototype, instance, whatever. ‣ Advice allows functional mixins to augment existing functions, not clobber them. ‣ Works with the language, simple to understand, no surprises. Debuggable.
Overview ‣ Functional mixins and advice/AOP are the shit ‣ Make use of JavaScript’s biggest strength - functions ‣ Extremely simple ‣ Endless flexibility ‣ Drop your class implementations and use this stuff now