Classes, Inheritance, and Functional Programming - mattoattacko/class GitHub Wiki

Delegation/Differential Inheritance

-Delegate prototype is an object that serves as a base for another object, and when you inherit from it, the new object gets a reference to the prototype. -When accessing a new object's property, the object's property is checked first. If not found, a check is then run on the prototype chain until it gets to the root delegate from most objects, the 'object.prototype'. -Useful because we only need one copy of each method to be shared by all instances, and thus preserves memory resources. -Can avoid property delegation by setting the prototype to “null” via “object.create(null)”. -Delegation is not as useful when we need to store a state as once you store it, any changes to members of the object/array will mutate all instances that share the prototype we made.

Concatenative Inheritance/Cloning/Mixins

-Concatenative inheritance is the process of copying the properties from one object to another, w/o retaining a reference between the two objects. -We can store the default state for objects with cloning, usually with “object.assign()” or “.extend()” methods.

Functional Inheritance

-Uses a factory function and uses concatenative inheritance to tack on new properties. -Functional Mixins: functions created to extend existing objects, allowing us to use function closure to encapsulate private data and make our private state enforaceable. -Privileged method: methods defined within the closure's function scope, giving them access to the private data.

Composition Over Class Inheritance

  • Best to never use class inheritance.
  • Composition is like a guitar pedal-board. It's simpler, more expressive and flexible.

Stamps

-Are composable factory functions -Stamp: composable factory function that returns object instances based on its descriptor. -Composable: a stamp or a plain old JS object stamp descriptor. -The most popular prototype-based inheritance library for JS (Stampit)

Descriptor

-Composable Descriptor: a meta data object which contains the information necessary to create an object instance. Contains: methods, properties, initializer, staticProperties.