Книга: Functional Programming in JavaScript
Назад: Function declarations
Дальше: The function constructor

Function expressions

Named functions can also be defined as an expression by defining an anonymous function and assigning it to a variable.

var bar = function(n, m) {   console.log(n*m); };

They are not hoisted like function declarations are. This is because, while function declarations are hoisted, variable declarations are not. For example, this will not work and will throw an error:

bar(2,3); var bar = function(n, m) {   console.log(n*m); };

In functional programming, we're going to want to use function expressions so we can treat the functions like variables, making them available to be used as callbacks and arguments to higher-order functions such as functions. Defining functions as expressions makes it more obvious that they're variables assigned to a function. Also, if we're going to write functions in one style, we should write all functions in that style for the sake of consistency and clarity.

Назад: Function declarations
Дальше: The function constructor

bsn
thank
Vesa Karvonen
I hope you don't mind, but I’d like to point you and your readers to my high-performance optics library for JavaScript that is in production use in multiple projects, has comprehensive support for partial optics and interactive documentation: https://calmm-js.github.io/partial.lenses/ (this takes a moment to load — be patient!)