JSFiddle - React, Tailwind, and code Playground
JavaScript
function Class() {}
Class.extend = function extend(props) {
var prototype = new this();
var _super = this.prototype;
for (var name in props) {
if (typeof props[name] == "function"
&& typeof _super[name] == "function") {
prototype[name] = (function (super_fn, fn) {
return function () {
var tmp = this.callSuper;
this.callSuper = super_fn;
var ret = fn.apply(this, arguments);
this.callSuper = tmp;
if (!this.callSuper) {
delete this.callSuper;
}
return ret;
}
})(_super[name], props[name])
} else {
prototype[name] = props[name];
}
}
function Class() {}
Class.prototype = prototype;
Class.prototype.constructor = Class;
Class.extend = extend;
Class.create = Class.prototype.create = function () {
var instance = new this();
if (instance.init) {
instance.init.apply(instance, arguments);
}
return instance;
}
return Class;
}
console.clear();
var Human = Class.extend({
init: function () {
this.nature = "Human";
},
say: function () {
console.log("I am a human");
}
})
var human = Human.create();
console.log(human);
human.say();
var Man = Human.extend({
init: function () {
this.callSuper();
this.sex = "man";
},
say: function () {
this.callSuper();
console.log("I am a man");
}
});
var man = Man.create();
console.log(man);
man.say();
var Person = Man.extend({
init: function () {
this.callSuper();
this.name = "lee";
},
say: function () {
this.callSuper();
console.log("I am Lee");
}
})
var person = Person.create();
console.log(person);
person.say();