=> Object Object.prototype === Object.getPrototypeOf(Function.prototype); // => true One may as well set properties on them: function foo() {...} foo.sampleProp = 'bar'; foo.sampleProp; // => 'bar'
do not modify external state // impure function function namePerson(person) { return Object.defineProperty(person, 'name', {value: 'Joe'}); } // pure function function namePerson(person) { return Object.assign({}, person, {name: 'Joe'}); }
of another function. f( g(x) ) (f ° g)(x) The simplest example of composition in JavaScript. function doStuff(arg) {...} function doMoreStuff(arg) {...} [...].map((x) => doMoreStuff(doStuff(x))); [...].map(doStuff).map(doMoreStuff);
argument to functions of one argument. — Haskell B. Curry // ES5 function add(a) { return function (b) { return a + b; }; } // ES6 const add = (a) => (b) => a + b; const sum = add(2)(4); // => 6
// ES5 function printSuperhero(a, b) { return function (c) { return a + ' ' + b + ' is ' + c; }; } // ES6 const printSuperhero = (a, b) => (c) => `${a} ${b} is ${c}`; printSuperhero('Clark', 'Kent')('Superman'); // => 'Clark Kent is Superman'
foo = true; let i = 1; let bar = false; let j = 10; // DON'T // DON'T if (equals(foo, bar)) { while (lt(i, j)) { ... i = inc(i); } } // DO // DO if (foo === bar) { while (i < j) { ... ++i; } }