Notoriously Psychedelic Modules
Miss any of our Open RFC calls?Watch the recordings here! »

inherits-ex

1.2.3 • Public • Published

Inherits-Ex npm

Join the chat at https://gitter.im/snowyu/inherits-ex.js

Build Status Code Climate Test Coverage downloads license

Browser-friendly enhanced inheritance fully compatible with standard node.js inherits and coffee-script.

This package modifies and enhances the standard inherits from node.js util module in node environment. It also has a shim for old browsers with no Object.create support.

Differs from the standard implementation is:

  • coffee-script supports
  • static inheritance
  • multi-inheritances(inheritance chain) supports
  • inherits at anytime.
    • you can not declare method/property before inherits in the standard way for it will replace the prototype object.
  • duplication inheritance check
  • Es6 Class supports
  • more helper functions

The standard inherits implementation is in inherits-ex/lib/inheritsDirectly, of casue it's the coffee-script supports and browser-friendly.

API

inherits(ctor, superCtor|superCtor[], staticInherit = true)

  • staticInherit (boolean): whether static inheritance,defaults to true.
  var inherits = require('inherits-ex/lib/inherits')

The enhanced inherits implementation.

  • coffee-script supports
  • multi-inheritances(inheritance chain) supports
  • inherits at anytime.
    • you can not declare method/property before inherits in the standard way for it will replace the prototype object.
  • duplication inheritance check
  • add the super_ property(the super ctor) to the ctor.
  • add the __super__ property(the super's prototype) to the ctor for the coffeeScirpt super keyword.
  • add the Class property(point to the current class) to the object's prototype.
    • just be care: the ctor may not be the current class.

usage

 
assert = require('assert')
inherits = require('inherits-ex/lib/inherits')
isInheritedFrom = require('inherits-ex/lib/isInheritedFrom')
log = console.log.bind console
 
class Root
  m: -> log('root')
 
class A
  inherits ARoot
  m: ->
    log('A')
    super
 
class B
  inherits BRoot
  m: ->
    log('B')
    super
 
class MyClass
  # MyClass -> A -> Root 
  inherits MyClassB
  # MyClass -> A -> B -> Root 
  inherits MyClassA
 
assert.notOk inherits(ARoot) #duplication inheritances prohibited. 
assert.ok isInheritedFrom(MyClassA)
assert.ok isInheritedFrom(MyClassRoot)
assert.ok isInheritedFrom(MyClassB)
 

and the following codes do same thing:

 
class Root
  @static: 1
  m: -> log('root')
 
class A
  m: ->
    log('A')
    super
 
class B
  m: ->
    log('B')
    super
 
class MyClass
  # create inheritances chain: 
  # MyClass -> A -> B -> Root 
  inherits MyClass[ABRoot]
 
assert.ok isInheritedFrom(MyClassA)
assert.ok isInheritedFrom(MyClassRoot)
assert.ok isInheritedFrom(MyClassB)
assert.equal MyClass.static1

inheritsDirectly(ctor, superCtor, staticInherit = true)

  • staticInherit (boolean): whether static inheritance,defaults to true.
  var inheritsDirectly = require('inherits-ex/lib/inheritsDirectly')

The standard inherits implementation in node.js environment with coffee-script supports and browser-friendly.

isInheritedFrom(ctor, superCtor|superCtorName, raiseError=false)

  var isInheritedFrom = require('inherits-ex/lib/isInheritedFrom')

return the superCtor's son if ctor is inherited from superCtor. else return false.

it will use the ctor.name to check whether inherited from superCtorName.

mixin(ctor, superCtor|superCtor[], options:{ filter: number|function})

  • options:
    • filter: defaults to 0.
      • 0: copy all properties(methods)
      • 1: raise error if found a method using super
      • 2: skip these methods which using super
      • string[]: only name in the array of string will be copied.
      • function(name, value){return value} the callback function of filter.
        • name: the property name
        • value: the property value.
  var mixin = require('inherits-ex/lib/mixin')

mixin all superCtors to ctor.

  • duplication mixin or inheritance check
  • NOTE::the methods in mixins using super() will jump to the old class(not stay on the class).
  • The mixined properties(methods) are cloned(copied) from superCtors
  • The all mixined properties(methods) are the first parent's ctor(MixinCtor_)
    • eg, ctor -> MixinCtor_ -> original parents
## Coffee@2.x 
mCallOrder = []
class Root
 
class C extends Root
  m: ->
    mCallOrder.push 'C'
    super
 
class A
  m: ->
    mCallOrder.push 'A'
 
class A1 extends A
  m: ->
    mCallOrder.push 'A1'
    super
 
class B
  inherits BRoot
 
class B1 extends B
  m: ->
    mCallOrder.push 'B1'
    super
 
mixin(B1[A1C]).should.be.equal true'mixin'
= new B1()
o.m("a"12) # call chain: B1::m -> C::m 
A::m.should.have.been.calledOnce
A::m.should.have.been.calledWith "a"12
mCallOrder.should.be.deep.equal ['B1''C']

The inheritance chain: B1 -> MixinCtor_ -> B -> Root All mixins will be added to MixinCtor_.

isMixinedFrom(ctor, superCtor|superCtorName)

  var isMixinedFrom = require('inherits-ex/lib/isMixinedFrom')

createObject(ctor, args...)

The helper function to create the object dynamically.

  var createObject = require('inherits-ex/lib/createObject')

NOTE: DO NOT SUPPORT ES6 Class

usage

 
class RefObject
  constructor: -> @initialize.apply @arguments
class MyObject
  inherits MyObjectRefObject
  initialize: (@a,@b)->
    super
 
obj = createObject(MyObject"a""b")
#obj = new MyObject("a", "b") # it will have no property a and b. 
assert.equal obj."a"
assert.equal obj."b"
 
 

createObjectWith(ctor, [args...])

The helper function to create the object dynamically.

  var createObjectWith = require('inherits-ex/lib/createObjectWith')

NOTE: DO NOT SUPPORT ES6 Class

createFunction(name, [args,] body[, scope[, values]])

  • arguments:
    • name (String): the function name
    • args (Array): the function argument list. it's optional.
    • body (String): the function body.
    • ``scope` (Object|Array): the optional function scope.
      • ignore the values if it's an object.
      • the value is requierd if it's an array. It's the key's name list
    • value (Array): the optional function scope's value list. only for the scope is the Array.

The helper function to create the function dynamically.

  var createFunction = require('inherits-ex/lib/createFunction')

usage

 
class RefObject
  constructor: -> @initialize.apply @arguments
class MyObject
  inherits MyObjectRefObject
  initialize: (@a,@b)->
    super
 
obj = createObject(MyObject"a""b")
#obj = new MyObject("a", "b") # it will have no property a and b. 
assert.equal obj."a"
assert.equal obj."b"
 
 

Install

npm i inherits-ex

DownloadsWeekly Downloads

12,851

Version

1.2.3

License

MIT

Unpacked Size

111 kB

Total Files

79

Last publish

Collaborators

  • avatar