Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
inherits-ex
Advanced tools
Browser-friendly enhanced inheritance fully compatible with standard node.js and coffee-script
Browser-friendly enhanced inheritance fully compatible with standard node.js inherits with dynamic inheritance or creation.
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:
isInheritedFrom(ctor, superCtor|superCtorName)
Check the ctor whether inherited from superCtormixin(ctor, superCtor|superCtor[])
Mixin the methods and properties of the SuperCtor:
isMixinedFrom(ctor, superCtor|superCtorName)
Whether mixined from superCtorcreateCtor(name, args, body)
Create Ctor(Class) dynamicallycreateObject(ctor, args...)
Create Object instance dynamicallycreateFunction(name, [args,] body[, scope[, values]])
Create Function dynamicallyThe standard inherits
implementation is in inherits-ex/lib/inheritsDirectly
,
of cause it's the coffee-script supports and browser-friendly.
staticInherit
(boolean): whether static inheritance,defaults to true.const inherits = require('inherits-ex/lib/inherits')
The enhanced inherits
implementation.
super_
property(the super ctor) to the ctor.__super__
property(the super's prototype) to the ctor for the coffeeScirpt super
keyword.Class
property(point to the current class) to the object's prototype.
The default constructor(empty) chain failed for ES6 Class can not call constructor directly if no Reflect.construct(target, args, newTarget)
native supports
const inherits = require('inherits-ex/lib/inherits')
const getPrototypeOf = require('inherits-ex/lib/getPrototypeOf')
// Or use function class instead of ES6 class:
// function Root() {this.initialize.apply(this, arguments)}
// Root.prototype.initialize = function initialize() {console.log('Root.init')}
class Root {
constructor() {
this.initialize.apply(this, arguments)
}
initialize() {
console.log('Root.init')
}
}
class A {
/*
// Workaround: must add the constructor in the derived class if you use inherits
// Or use function Root instead of ES6 class
// MUST BE EXISTS constructor to call parent constructor if it's ES6 class
// */
constructor() {
if (!this.Class) this.Class = getPrototypeOf(this).constructor
const Parent = getParentClass(this.Class)
// create the instance by calling the parent constructor
return Reflect.construct(Parent, arguments, this.Class)
// or call initialize method directly
// this.initialize.apply(this, arguments)
}
initialize() {
const ParentPrototype = this.Class.__super__
ParentPrototype.init.apply(this, arguments)
console.log('A.init')
}
}
inherits(A, Root)
const obj = new A() // Bug: The initialize method can not be executed.
# Coffee@1
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 A, Root
m: ->
log('A')
super
class B
inherits B, Root
m: ->
log('B')
super
class MyClass
# MyClass -> A -> Root
inherits MyClass, B
# MyClass -> A -> B -> Root
inherits MyClass, A
assert.notOk inherits(A, Root) #duplication inheritances prohibited.
assert.ok isInheritedFrom(MyClass, A)
assert.ok isInheritedFrom(MyClass, Root)
assert.ok isInheritedFrom(MyClass, B)
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, [A, B, Root]
assert.ok isInheritedFrom(MyClass, A)
assert.ok isInheritedFrom(MyClass, Root)
assert.ok isInheritedFrom(MyClass, B)
assert.equal MyClass.static, 1
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.
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 the methods and properties of the SuperCtor: Clone(Copy) all superCtor
's properties(methods) to ctor.
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.
var mixin = require('inherits-ex/lib/mixin')
mixin all superCtors to ctor.
super()
will jump to the old class(not stay on the class).MixinCtor_
)
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 B, Root
class B1 extends B
m: ->
mCallOrder.push 'B1'
super
mixin(B1, [A1, C]).should.be.equal true, 'mixin'
o = 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_
.
check the ctor whether is mixined from superCtor.
var isMixinedFrom = require('inherits-ex/lib/isMixinedFrom')
Create a constructor(class) dynamically.
name
(string): the class nameargs
(any[]): the optional constructor's args.body
(string): the optional constructor function body. const createClass = require('inherits-ex/lib/createCtor')
const MyClass = createClass('MyClass', ['a', 'b'], 'this.sum = a + b');
var my = new MyClass(1, 2);
console.log(my.sum);
The helper function to create the object dynamically and arguments provided individually.
var createObject = require('inherits-ex/lib/createObject')
class MyClass {
constructor(a,b) {
this.sum = a + b;
}
}
var o = createObject(MyClass, 1, 2)
console.log(o.sum)
NOTE: It will call the parent constructor if the class is the Empty constructor.
var inherits = require('inherits-ex/lib/inherits')
var createObject = require('inherits-ex/lib/createObject')
class Root {
constructor() {
this.init = 'root'
}
}
class MyClass {
}
inherits(MyClass, Root)
var obj = createObject(MyClass)
assert.equal(obj.init, 'root')
Usage:
class RefObject
constructor: -> @initialize.apply @, arguments
class MyObject
inherits MyObject, RefObject
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 "a"
assert.equal obj.b "b"
The helper function to create the object dynamically. provides the arguments as an array (or an array-like object).
var createObjectWith = require('inherits-ex/lib/createObjectWith')
var obj = createObjectWith(MyObject, ['a', 'b'])
NOTE: It will call the parent constructor if the class is the Empty constructor.
name
(String): the function nameargs
(Array): the function argument list. it's optional.body
(String): the function body.values
if it's an object.value
is required if it's an array. It's the key's name listvalue
(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 MyObject, RefObject
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 "a"
assert.equal obj.b "b"
FAQs
Enhanced inheritance for dynamic inheritance and mixin.
The npm package inherits-ex receives a total of 53,981 weekly downloads. As such, inherits-ex popularity was classified as popular.
We found that inherits-ex demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.