November 2017
Intermediate to advanced
386 pages
9h 22m
English
1.1. Classes as first-class objects: We saw that functions are first class objects, but did you know classes also are? (Though, of course, speaking of classes as objects does sound weird...) Study this example and see what makes it tick! Be careful: there's some purposefully weird code in it:
const makeSaluteClass = term => class { constructor(x) { this.x = x; } salute(y) { console.log(`${this.x} says "${term}" to ${y}`); } }; const Spanish = makeSaluteClass("HOLA"); new Spanish("ALFA").salute("BETA"); // ALFA says "HOLA" to BETA new (makeSaluteClass("HELLO"))("GAMMA").salute("DELTA"); // GAMMA says "HELLO" to DELTA const fullSalute = (c, x, y) => new c(x).salute(y); const French = makeSaluteClass("BON JOUR"); fullSalute(French, ...