Instsure is a function wrapper to ensure the object initialitation, preventing wrong callings to the constructor function without right context.
Install with npm install instsure --save
.
To use, add the require
node module or use browserify if you want to use in a browser:
// Wrapper Function
const Instsure = require('instsure');
var ConstructorWrapFoo = Instsure(function() {
this.args = arguments;
this.context = this;
});
ConstructorWrapFoo.prototype.foo = 'bar'
var instance = new ConstWrapFoo(1,2,3,4),
// or
instance = ConstWrapFoo(1,2,3,4),
// or
instance = ConstWrapFoo.new([1,2,3,4]);
console.log(instance.args) // [1,2,3,4];
console.log(instance instanceof ConstWrapFoo); // true
// Existing Constructor
const Instsure = require('instsure');
var ConstructorFoo = function() {
this.args = arguments;
this.context = this;
};
ConstructorWrapFoo.prototype.foo = 'bar'
var instance = new Instsure(ConstructorFoo)(1,2,3,4),
// or
instance = Instsure(ConstructorFoo)(1,2,3,4),
// or
instance = Instsure(ConstructorFoo).new([1,2,3,4]);
console.log(instance.args) // [1,2,3,4];
console.log(instance instanceof ConstructorFoo); // true
console.log(instance instanceof ConstructorWrapFoo); // true