JSFiddle - React, Tailwind, and code Playground
by silv3r_m00n
JavaScript
/*
http://www.crockford.com/javascript/inheritance.html
Output should only be
B bing
A bing
There is an extra B bing because each function is copied to the inheriting class.
*/
Function.prototype.method = function (name, func)
{
this.prototype[name] = func;
return this;
};
Function.method('inherits', function (parent) {
this.prototype = new parent();
for(var i in this.prototype)
{
if(typeof this.prototype[i] == 'function')
{
//bind by value to closure
(function(name, ob) {
ob.prototype[name] = function()
{
//var k = this.uber(name, Array.prototype.slice.apply(arguments, [0]));
var params = arguments;
params.unshift(name);
return this.uber.apply(this, params );
return k;
}
})(i, this);
}
}
var d = {},
p = this.prototype;
this.prototype.constructor = parent;
this.method('uber', function uber(name) {
if (!(name in d)) {
d[name] = 0;
}
var f, r, t = d[name], v = parent.prototype;
if (t) {
while (t) {
v = v.constructor.prototype;
t -= 1;
}
f = v[name];
} else {
f = p[name];
if (f == this[name]) {
f = v[name];
}
}
d[name] += 1;
r = f.apply(this, Array.prototype.slice.apply(arguments, [1]));
d[name] -= 1;
return r;
});
return this;
});
//PARENT - A
function A()
{
this.type = 'A';
}
A.prototype.bing = function()
{
document.write('A bing <br />');
}
A.prototype.print_type = function()
{
document.write(this.type + ' <br />');
}
//CHILD - B
function B()
{
this.type = 'B';
}
B.inherits(A);
B.prototype.bing =...