JSFiddle - React, Tailwind, and code Playground
JavaScript
// http://daniel.steigerwald.cz (MIT Licensed)
var $class = function(definition) {
var constructor = definition.constructor;
var parent = definition.Extends;
if (parent) {
var F = function() { };
constructor._superClass = F.prototype = parent.prototype;
constructor.prototype = new F();
}
for (var key in definition) {
constructor.prototype[key] = definition[key];
}
constructor.prototype.constructor = constructor;
return constructor;
};
// třída Person
var Person = $class({
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 otestujeme
// vytvoříme instanci
var joe = new Employee('Joe', 1000);
// všechny tyto testy musí projít
alert([
joe.getName() == 'Joe (zaměstnanec)',
joe.getSalary() == 1000,
joe instanceof Person,
joe instanceof Employee,
typeof Employee == 'function',
typeof joe == 'object',
joe.constructor == Employee,
Employee._superClass == Person.prototype
]);