Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
This library provides simple but very extensible way to define types / classes / constructors in JS. It comes a very small set of functions to do that, but any function can be incorporated to allow more complex definitions.
It's quite common to have constructors that just set given arguments as properties of the instance, providing rest of the API through prototype methods. This library makes this task simple by reducing all the machinery code:
var Point = define(record, [ 'x', 'y' ])
Code above will produce constructor which is equivalent of:
var Point = function Point(x, y) {
this.x = x
ths.y = y
}
Commonly you will want to define some methods for the Point
type, that
can be done as follows:
var Point = define(
record, [ 'x', 'y' ],
implement, {
clear: function() {
this.x = 0
this.y = 0
}
})
You could also extend existing types in similar way:
var Pixel = define(
record, [ 'x', 'y', 'color' ],
extend, Point,
implement, {
draw: function() {
// ...
}
})
Now record
is limited to a very specific scenarios, but more generic use
cases are also covered:
var Dog = define(
construct, function(options) {
this.name = options.name
// ...
},
extend, Animal,
implement, {
bark: function() {
// ....
}
})
You could also refine existing types in a same way:
define(
over, EventEmitter,
implement, {
twice: function(type, listener) {
var times = 0
this.on(type, listener)
this.on(type, function limiter() {
if (++ times === 2) {
this.removeListener(type, listener)
this.removeListener(type, limiter)
}
})
}
})
While this tiny library provides some core functionailty for building and
extending types / constructors / classes, there is no way it could cover all
use cases. That's not an issues since all the functionality is defined in
via simple functions like extend
, implement
, record
... You could
incorporate your own functions to do more sophisticated things. For example
adding aspect oriented programming
concepts is very easy:
function after(type, methods) {
var prototype = type.prototype
Object.keys(methods).forEach(function(name) {
var before = prototype[name]
var after = methods[name]
prototype[name] = function() {
before.apply(this, arguments)
return after.call(this)
}
})
return type
}
var Point3D = define(
record, [ 'x', 'y', 'z' ],
extend, Point,
after, {
clear: function() {
this.z = 0
}
})
// EventEmitter2
define(
over, EventEmitter,
after, {
emit: function(type) {
if (type !== '*')
this.emit.apply(this, ['*'].concat(Array.prototype.slice.call(arguments))
}
})
You could in fact do so much more! Enforce API contracts using guards, define extensions on built-ins types without changing their prototypes via [type based dispatch] (https://github.com/Gozala/protocol) or do a [pattern based dispatch] (https://github.com/Gozala/dispatcher/) or maybe something entirely new!
npm install alter-ego
0.0.1 / 2012-05-05 ##
FAQs
Extensible type definitions
The npm package alter-ego receives a total of 1 weekly downloads. As such, alter-ego popularity was classified as not popular.
We found that alter-ego demonstrated a not healthy version release cadence and project activity because the last version was released 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.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.