JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 102
  • Score
    100M100P100Q63072F
  • License ISC

A lightweight JavaScript inheritance utility.

Package Exports

  • lighter-type

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (lighter-type) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

lighter-type

Chat Version Downloads Build Coverage Style

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

API

The lighter-type module outputs a constructor with several methods.

Type.extend(map)

Define and return a sub type of the Type object, with a prototype decorated with a map of additional properties. Additionally, the sub type itself gets the same properties as its super type (such as the extend method).

When the map 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 = require('lighter-type')

// Make a Person type.
var Person = Type.extend({

  // Construct a new person with a name.
  init: function (name) {
    this.name = name
  },

  // Give each person a default salutation of "Hello".
  salutation: 'Hello',

  // Greet a person with their defined salutation.
  greet: function () {
    console.log(this.salutation + ', ' + this.name + '!')
  }

})

// Make a Friend sub type by extending the Person type.
var Friend = Person.extend({

  // Be a bit more informal with friends.
  salutation: 'Hi'

})

// Instantiate Bob, and greet him.
var bob = new Person('Bob')
bob.greet()
//> "Hello, Bob!"

// Instantiate Joe, and greet him.
var joe = new Friend('Joe')
joe.greet()
//> "Hi, Joe!"

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 = require('lighter-type')

var Robot = Type.extend({
  bend: function (object) {
    object.isBent = true
  }
})

var Bender = Robot.extend({
  bend: function (object) {
    this._super.bend(object)
    console.log('Bite my shiny metal ass.')
  }
})

var beam = {}
var bender = new Bender()
bender.bend(beam)
//> Bite my shiny metal ass.

console.log(beam.isBent)
//> 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 = require('lighter-emitter')

// Make the `console` object into an emitter.
Emitter.init(console)

console.on('hi', function (name) {
  console.log('Hi, ' + name + '!')
})

console.emit('hi', 'Sam')
//> 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 = require('lighter-type')

// Add a few methods to the Array object's prototype.
Type.decorate(Array.prototype, {
  first: function () {
    return this[0]
  },
  last: function () {
    return this[this.length - 1]
  },
  sum: function () {
    var s = 0
    for (var i = 0, l = this.length; i < l; i++) {
      s += this[i]
    }
    return s
  }
})

// Create a plain old array of numbers.
var a = [1, 2, 3]

console.log(a.first())
//> 1

console.log(a.last())
//> 3

console.log(a.sum())
//> 6

The decorate method can be used for multiple inheritance purposes, by using multiple Type prototypes to decorate another object prototype.

var Type = require('lighter-type')

// A vehicle might work on land or water.
var Vehicle = Type.extend({

  // Return isLandVehicle as a boolean.
  worksOnLand: function () {
    return !!this.isLandVehicle
  },

  // Return isWaterVehicle as a boolean.
  worksOnWater: function () {
    return !!this.isWaterVehicle
  }

})

// A car is a land vehicle.
var Car = Vehicle.extend({
  isLandVehicle: true
})

// A boat is a water vehicle.
var Boat = Vehicle.extend({
  isWaterVehicle: true
})

// A hovercraft is a vehicle, plain and simple.
var Hovercraft = Vehicle.extend({})

// Make it work like a car or a boat.
Car.decorate(Hovercraft)
Boat.decorate(Hovercraft)

// Create a new Hovercraft.
var hovercraft = new Hovercraft()

console.log(hovercraft.worksOnLand())
//> true

console.log(hovercraft.worksOnWater())
//> true

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 = require('lighter-type')

// Create an object with a visible (i.e. enumerable) method.
var object = {
  visible: function () {
    console.log('I am visible.')
  }
}

// Add a non-enumerable property called "hidden".
Type.hide(object, 'hidden', function () {
  console.log('I am hidden.')
})

// Verify that the "hidden" method exists.
object.hidden()
//> "I exist."

// Verify that only the "visible" method is enumerable.
for (var key in object) {
  console.log(key)
}
//> "visible"

More on lighter-type...