JSFiddle - React, Tailwind, and code Playground
HTML
<script>
var Class = {
/**
*allocate
* Creates an instance. Default is
* the implementation of native `new'
*/
allocate: function () {
return Object.create(this);
},
/**
* new
* A wrapper over allocated and initialize
*/
new: function () {
var instance = this.allocate();
this.initialize.apply(instance, arguments);
return instance;
},
/**
* initialize
* Configure a newly created instance
*/
initialize: function () {},
/**
* super
*/
super: function (method) {
var parentName = '__' + method + '_invoke__';
var parentObject = this[parentName];
var proto;
if (parentObject) {
// deeper levels
proto = Object.getPrototypeOf(parentObject.proto);
} else {
// first level (proto of an instance) -- get the next level
proto = Object.getPrototypeOf(Object.getPrototypeOf(this));
}
this[parentName] = {proto: proto, level: (parentObject ? parentObject.level++ : 0)};
var result = proto[method].apply(this, [].slice.call(arguments, 1));
this[parentName].level--;
if (!this[parentName].level)
delete this[parentName];
return result;
}
};
</script>
JavaScript
var One = Object.create(Class, {
one: { value: function () {
return this.two();
}},
two: { value: function () {
return "you win";
}}
});
var Two = Object.create(One, {
one: { value: function () {
return this.super("one");
}},
two: { value: function () {
return "broken";
}}
});
var instance = Object.create(Two);
console.log(instance.one());