lighter-type
The lighter-type
module is a prototypal inheritance utility with
better performance
than similar inheritance utilities.
It supports:
- Constructors
- Prototypal inheritance
- Multiple inheritance
- Non-enumerable property definitions
Installation
From your project directory, install and save as a dependency:
npm install --save lighter-type
Usage
The lighter-type
module outputs a constructor with several methods.
- Type.extend(prototypeProps[, constructorProps])
- Type.init(object[, overwrite][, args])
- Type.decorate(object[, map][, overwrite])
- Type.include(type[, overwrite])
- Type.is(type)
- Type.has(type)
- Type.hide(object, key, value)
Type.extend([constructor], [prototypeProps], [constructorProps])
Define and return a sub type of the Type
object, with a prototype decorated
with optional prototypeProps
(a map of additional prototype properties) and optional
constructorProps
(a map additional type properties. The sub type itself also inherits
the properties of its super type (such as the extend
method).
When the prototypeProps
argument includes a property called init
, it is used as the
constructor for the sub type rather than being added as a prototype property.
var Type = // Construct a new person with a name.var Person = Type // Make a Friend sub type by extending the Person type.var Friend = Person // Instantiate Bob, and greet him.var bob = 'Bob'bob//> "Hello, Bob!" // Instantiate Joe, and greet him.var joe = 'Joe'joe//> "Hi, Joe!" // Make sure a Friend is a Friend.console//> "Friend"
Each type's prototype has _super
property which references its parent
prototype, and each type has a _super
property which references its
parent type.
var Type = var Robot = Type var Bender = Robot var beam = {}var bender = bender//> Bite my shiny metal ass. console//> true
Type.init(object[, overwrite][, args])
Decorate an object with the prototype of a Type
, and call its constructor
on the object with an args
array, unless args
is false
, in which case
the constructor will be skipped.
Any object that extends Type (such as
lighter-emitter)
will inherit Type.init
and other Type methods.
// The `lighter-emitter` module extends `lighter-type`.var Emitter = // Make the `console` object into an emitter.Emitter console console//> Hi, Sam!
Type.decorate(object[, map][, overwrite])
Decorate an object
with a map
of additional properties (or overriding
properties if overwrite
is truthy). If the map is not specified, the Type
prototype will decorate the object
prototype instead.
var Type = // Add a few methods to the Array object's prototype.Type // Create a plain old array of numbers.var a = 1 2 3 console//> 1 console//> 3 console//> 6
Type.include(type[, overwrite])
Implement multiple inheritance by decorating one Type's prototype with the prototype properties of another.
var Type = // A vehicle might work on land or water.var Vehicle = Type // A car is a land vehicle.var Car = Vehicle // A boat is a water vehicle.var Boat = Vehicle // A hovercraft is also a vehicle.var Hovercraft = Vehicle // A hovercraft includes the functionality of a Car and a Boat.HovercraftHovercraft // Create a new Hovercraft.var hovercraft = console//> true console//> true
Type.is(type)
Check whether this Type is descended from another Type.
var Foo = Typevar Bar = Foovar Baz = Bar console//> true console//> false
Type.has(type)
Check whether this Type has acquired the functionality of another type via the extend method or the include method.
var Type = // Create an object with a visible (i.e. enumerable) method.var object = { console } // Add a non-enumerable property called "hidden".Type // Verify that the "hidden" method exists.object//> "I exist." // Verify that only the "visible" method is enumerable.for var key in object console//> "visible"
Type.hide(object, key, value)
Create a property of object
named key
with value value
, and "hide" it by
making it non-enumerable.
var Type = // Create an object with a visible (i.e. enumerable) method.var object = { console } // Add a non-enumerable property called "hidden".Type // Verify that the "hidden" method exists.object//> "I exist." // Verify that only the "visible" method is enumerable.for var key in object console//> "visible"