super
Installation
Super is available for both server-side and the browser.
Node.js
Package is available through npm:
npm install @js-npm/super
Getting Started
It can be used is a replacement for node's util.inherits
. Especially useful when building
modules for both node and the browser.
const inherits = require('super');
// Classes, actually it uses util.inherits.
const EventEmitter = require('events').EventEmitter;
function Foo() {};
inherits(Foo, EventEmitter);
let foo = new Foo;
console.log(foo instanceof EventEmitter); // true
console.log(Foo.super_ == EventEmitter); // true
It can also be used for simple merging or cloning of objects.
// merge
const foo = { bar: 'baz' },
bar = { foo: 3 },
baz = inherits(bar, foo);
console.log(baz);
// clone
const bar = { foo: 3 },
barClone = clone(bar);
And finally, it also provides a helper that will allow for object to easily be extended, similiar to the style in Backbone.js.
function Foo () {
this._constructed = true;
if (this.initialize) this.initialize();
}
Foo.extend = inherits.extend;
const Bar = Foo.extend({
initialize: function () {
this._isBar = true;
}
});
const foo = new Foo(),
bar = new Bar();
console.log(foo._constructed); // true
console.log(bar._constructed); // true
console.log(foo._isBar); // undefined
console.log(bar._isBar); // true
Tests
All:
make test
Node:
make test-node
Browser:
make test-browser
Test coverage:
make coverage
License
Apache License
Copyright (c) 2020 Alex alex@webz.asia
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.