JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
JavaScript
var inherit = function (name, parent, ctor, prototypeMembers) {
ctor = ctor || function () {};
prototypeMembers = prototypeMembers || {};
var augmentedCtor = function () {
// constructor-stealing
parent.apply(this, arguments);
ctor.apply(this, arguments);
};
augmentedCtor.prototype = function () {
// prototype chaining
var proto = Object.create(parent.prototype);
var publicApi = $.extend(proto, {
constructor: ctor,
_base_: Object.create(parent.prototype)
});
// extend
$.extend(publicApi, prototypeMembers);
return publicApi;
}();
var code = 'var ' + name + '=' + augmentedCtor +
';' + name + '.prototype = augmentedCtor.prototype' +
';(function() { return ' + name + ' }())';
return eval(code);
};
var allclasses = {};
var Base = function () {
this.name = 'base';
this.varname = 250;
}
Base.prototype = {
foo: function () {
this.varname += 50;
}
}
allclasses['Base'] = Base;
var create = function (opts) {
var ParentCtor = allclasses[opts.parentName],
ChildCtor = allclasses[opts.name];
if (!ParentCtor || ChildCtor) {
return false;
}
allclasses[opts.name] = inherit(opts.name, ParentCtor, opts.childCtor, opts.childProto);
return allclasses[opts.name];
}
var myclass = create({
parentName: 'Base',
name: 'specialname',
childCtor: function () { },
childProto: {
foo: function(){
this.varname += 100;
this._base_.foo();
return this.varname;
}
}
});
var myobject = new myclass();