Phenotype
"A phenotype (from Greek phainein, 'to show' + typos, 'type') is the composite of an organism's observable characteristics or traits" -- Wikipedia
Phenotype is a modern typing system for Javascript based on "Traits".
The main idea behind Traits is taking SRP to the typing system by decoupling:
- The object type hierarchy (inheritance and polimorphism); and
- Behavior definition (composable units of reusable behavior)
A Trait is, on its simplest form, a set of methods (behavior). They provide the ultimate reusability of multiple inheritance, but without all the feared sloppyness (eg. unexpected/uncontrolled overrides, and the famous diamond problem).
Phenotype traits extend the typical definition of Trait, to use them as the only type artifact (instead of traits being just a complement for classes).
This makes them simpler to use, and better matches the prototypal nature of Javascript.
Installation
npm install phenotype
Introductory Examples
var Trait = phenotypeTrait;var Walker = 'Walker'{phenotype;};var Swimmer = 'Swimmer'{phenotype;};var Flyer = 'Flyer'{phenotype;};var Duck = 'Duck' Walker Swimmer Flyer;tryDuck;catcherr// conflict, goTo is defined in Walker, Swimmer and Flyerconsole;// updatingvar jet = Flyer;// add a trait to existing objectvar Vehicle = 'Vehicle' seats: 3 ;Vehicle;// logs 3console;// modify existing TraitVehicle;// logs 4 2 trueconsole;// using "required"var Retriever = 'Retriever'goTo: phenotypememberrequiredgrab: phenotypememberrequired{this;this;this;};tryvar retriever = Retriever;catcherr// goTo is required by Retriever, this is an abstract Traitconsole;var Dog = 'Dog' Walker Retriever{phenotype;};var dog = Dog;tryvar ball = {};dog;catcherr// throws "pending" error from grab method aboveconsole;// using mixin (allows to apply a Trait to a preexistent object)var parrot = name: 'pepe' ;tryRetriever;catcherr// goTo is required by Retriever, and parrot doesn't have itconsole;parrotgoTo = phenotypepending;parrotgrab = phenotypepending;// this time parrot provides all required methodsRetriever;// using "aliasOf" and "from"var Bird = 'Bird' Walker FlyerwalkTo: phenotypemembergoTo: phenotypemember;var Hawk = 'Hawk' Bird Retriever{phenotype;};var Capibara = 'Capibara' Walker SwimmerwalkTo: phenotypememberswimTo: phenotypemember{location ? this : this;};// using ancestorsvar Electric = 'Electric'{console;};var CombustionEngine = 'CombustionEngine'{console;};var Car = 'Car' Electric CombustionEngine{console;}shutdown: phenotypemember;var RetractableRoof = 'RetractableRoof'{console;}{console;};var ConvertibleCar = 'ConvertibleCar' Car RetractableRoofopen: phenotypemembershutdown: phenotypemember;// using pipe and asyncvar Peeler = 'Peeler'{console;// peeling takes time, but timeout at 1500msvar async = phenotype;;return async;};Peeler;var Chopper = 'Chopper'{console;return 'chopped ' + thing;};var Mixer = 'Mixer'{console;// mixing takes timevar async = phenotype;;return async;};var Oven = 'Oven'{console;return 'baked ' + thing;};var CookingMachine = 'CookingMachine' Peeler Chopper Mixer Ovenprocess: phenotypemember;var machine = CookingMachine;machine;// properties & eventsvar Labrador = 'Labrador' Dog Retriever phenotypeHasEventsname: phenotypememberinitial: phenotypemember;var spike = Labrador;spikename'Spike';// logs "Spike"console;// logs "S"console;spike;// logs "Spikey barked loud"spike;spike;spike;// logs "name changed from Spike to Spikey"spikename'Spikey';
Dynamic Prototypes
If available, phenotype will make use of ES6 Proxies, making created objects to inherit members dinamically, making Trait inheritance as dynamic as native prototypal inheritance.
At the moment of this writing ES6 Proxies are available at:
- Chrome (using harmony flag)
- FF
- Node.js (using harmony flag)
If ES6 Proxies are not available, phenotype will fallback to using fixed prototypes, ie. objects won't have members added to Traits after the object creation. This can be forced using MyTrait.fixed()
.
License
The MIT License (MIT)
Copyright (c) 2013 Benjamin Eidelman (@beneidel)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.