The fact that Functions are Objects that can have properties/methods is supremely undervalued.
Are there other languages that do this so nicely? It's the perfect blend of OO and functional.
Programming is mostly about gradually figuring out the right design I find. JS/TS let's me evolve things naturally without big rewrites.
function foo() {}
function bar() {}
function baz() {}
const commands = [foo, bar, baz]
// Run commands
commands.forEach(x => x())
foo.help = 'This does something'
// Describe commands
commands.forEach(x => console.log(x.help))
// Add some state via closure.
const config = {}
function foo() { config.blah }
// Add some state using partials.
function _foo(config) { config.blah }
const foo = foo.bind(null, config)
I can flexibly do what I need, without ever having to define a class like `Command`, which I probably don't even know what it should be yet.This premature naming of things in OO creates so many dramas.
It avoids things like `VideoCompressor#compress()`, `VideoSplitter#split()`. You don't have to think: what state belongs in which class...you just call the function and pass it what it needs.