define-js is a JavaScript library that introduces class-like inheritance for ES5, bridging the gap between prototype-based and class-based object modeling.
While ES5 prototypes and ES6 classes are thin objects (memory optimized, but prone to domain-modeling ambiguities), define-js produces fat objects (domain safe, encapsulating inheritance explicitly).
This design trades minimal memory footprint for clarity, correctness, and domain safety—an approach aligned with enterprise-grade software modeling.
npm install define-jsThe following example demonstrates how to implement inheritance using define-js, extending a constructor (SuperFunction), initializing with defaults, and overriding methods safely.
var define = require('define-js');
// Define a super constructor
var SuperFunction = function(value) {
var ivar = value;
this.method = function() {
console.log(ivar);
};
};
// Define a new function extending from SuperFunction
module.exports = define(function(init, sṵper) {
return function(value) {
// Initialize the super function with arguments and bind scope
var self = init(value).self(this);
// Alternative: var self = init.apply(this, arguments).self();
// Override a method from the super object
self.method = function() {
sṵper.method();
};
};
})
.extend(SuperFunction /* the function to inherit from */)
.defaults("Value" /* default argument passed when instantiating */);-
Thin Objects (ES5 / ES6)
- Share prototype references between instances.
- Highly memory efficient.
- Risk of semantic confusion in domain modeling.
-
Fat Objects (define-js)
- Encapsulate inheritance more explicitly, binding super and child roles inside the object.
- Higher memory cost for persisted state objects only.
- Safer for modeling business domains where object identity must remain unambiguous.
- Class-like inheritance in ES5
- Domain safety through fat object modeling
MIT
