JSFiddle - React, Tailwind, and code Playground
JavaScript
// http://daniel.steigerwald.cz (MIT Licensed)
var $class = function(def) {
// pokud není konstruktor definován, použijeme nový (nechceme použít zděděný)
var constructor = def.hasOwnProperty('constructor') ? def.constructor : function() { };
// proces vytváření třídy rozdělíme do kroků
for (var name in $class.Initializers) {
$class.Initializers[name].call(constructor, def[name], def);
}
return constructor;
};
$class.Initializers = {
Extends: function(parent) {
if (parent) {
var F = function() { };
this._superClass = F.prototype = parent.prototype;
this.prototype = new F;
}
},
Mixins: function(mixins, def) {
// kostruktoru přidáme metodu mixin
this.mixin = function(mixin) {
for (var key in mixin) {
if (key in $class.Initializers) continue;
this.prototype[key] = mixin[key];
}
this.prototype.constructor = this;
};
// a přidanou metodu hned využijeme pro rozšíření prototype
var objects = [def].concat(mixins || []);
for (var i = 0, l = objects.length; i < l; i++) {
this.mixin(objects[i]);
}
}
};
// mix Serializable
var Serializable = {
serialize: function() {
return 'serialized';
}
};
// třída Person
var Person = $class({
Mixins: Serializable,
constructor: function(name) {
this.name = name;
},
getName: function() {
return this.name;
}
});
// třída Employee
var Employee = $class({
Extends: Person,
constructor: function(name, salary) {
Person.call(this, name);
this.salary = salary;
},
// přepisujeme metodu z třídy Person
getName: function() {
var name = Employee._superClass.getName.call(this);
return name + ' (zaměstnanec)';
},
getSalary: function() {
return this.salary;
}
});
// Nyní kód...