January 2020
Intermediate to advanced
470 pages
11h 13m
English
1.1. Classes as first-class objects: We learned that functions are first-class objects, but did you know that classes also are? (Though, of course, speaking of classes as objects does sound weird.) Look at the following 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 BETAnew (makeSaluteClass("HELLO"))("GAMMA").salute("DELTA");// GAMMA says "HELLO" to DELTAconst fullSalute = (c, x, y) => new c(x).salute(y);const French = makeSaluteClass("BON JOUR");fullSalute(French, ...Read now
Unlock full access