Skip to content

Instantly share code, notes, and snippets.

@stephanschuler
Created January 3, 2017 09:58
Show Gist options
  • Save stephanschuler/66015dc23028bd8daa32fa5c46f7c760 to your computer and use it in GitHub Desktop.
Save stephanschuler/66015dc23028bd8daa32fa5c46f7c760 to your computer and use it in GitHub Desktop.
(function () {
function wrapConstructor(OriginalClass, OnInitFunc) {
const functionBody = `
const WrappereClass = function () {
OriginalClass.apply(this, arguments);
if (OnInitFunc) OnInitFunc.call(this);
};
WrappereClass.prototype = Object.create(OriginalClass.prototype);
WrappereClass.prototype.constructor = WrappereClass;
return WrappereClass;
`.replace(/WrappereClass/g, OriginalClass.name);
return (new Function('OriginalClass', 'OnInitFunc', functionBody))(OriginalClass, OnInitFunc);
}
(function App() {
function class1(a, b) {
this.a = a;
this.b = b;
}
let class2 = wrapConstructor(class1, function () {
console.log('after creating: ', this);
});
let value1 = new class2('a', 'b');
let value2 = new class2('c', 'd');
console.log({
value1: value1,
value2: value2,
instanceof_class1: value1 instanceof class1,
instanceof_class2: value1 instanceof class2
});
})();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment