@MichalZalecki
michalzalecki.com
woumedia.com
w językoznastwie: właściwy tylko jednemu językowi
‒ sjp.pl
class Person {
constructor(name) {
this.name = name;
}
hi() {
console.log(`Hi ${this.name}!`);
}
by() {
console.log(`By ${this.name}!`);
}
}
const p = new Person("Michal");
p.hi();
p.by();
class Person {
...
}
> Person.prototype
// {
// by: function by(),
// constructor: function Person(name),
// hi: function hi(),
// }
‒ Eric Elliot
‒ The Gang of Four
const HiMixin = Sup => class extends Sup {
hi() {
console.log(`Hi ${this.name}!`);
}
};
const ByMixin = Sup => class extends Sup {
by() {
console.log(`By ${this.name}!`);
}
};
class Person extends HiMixin(ByMixin(class {})) {
constructor(name) {
super(name);
this.name = name;
}
}
const p = new Person("Michal");
p.hi(); // "Hi Michal!"
p.by(); // "By Michal!"
function hi() { console.log(`Hi ${this.name}!`); }
function by() { console.log(`By ${this.name}!`); }
function mixin(fn, name = fn.name) {
return target => {
target.prototype[name] = fn;
};
}
@mixin(by)
@mixin(hi)
class Person {
constructor(name) {
this.name = name;
}
}
const p = new Person("Michal");
p.hi(); // "Hi Michal!"
p.by(); // "By Michal!"
‒ Douglas Crockford
function hi() {
console.log(`Hi ${this.name}!`);
}
function by() {
console.log(`By ${this.name}!`);
}
function mixin(fn, name = fn.name) {
return Object.defineProperty(this, name, { value: fn });
}
const p = { name: "Michal" }::mixin(by)::mixin(hi);
p.hi(); // "Hi Michal!"
p.by(); // "By Michal!"
import stampit from "stampit";
function hi() {
console.log(`Hi ${this.name}!`);
}
function by() {
console.log(`By ${this.name}!`);
}
const Person = stampit().methods({ hi, by });
const p = Person({ name: "Michal" });
p.hi(); // "Hi Michal!"
p.by(); // "By Michal!