From 3c6d40ccca6c436b7628c6fd3d2fe6a7eb9aae1a Mon Sep 17 00:00:00 2001 From: jabacchetta Date: Thu, 19 Oct 2017 19:46:28 -0500 Subject: [PATCH] [docs] Clarify reasoning for named function expressions For convenience, added brief explanations that are given in the linked discussion (mostly eliminating the need to scroll through the comments). --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f7d8a77e..f2fe1dc1 100644 --- a/README.md +++ b/README.md @@ -633,7 +633,7 @@ Other Style Guides - [7.1](#functions--declarations) Use named function expressions instead of function declarations. eslint: [`func-style`](http://eslint.org/docs/rules/func-style) jscs: [`disallowFunctionDeclarations`](http://jscs.info/rule/disallowFunctionDeclarations) - > Why? Function declarations are hoisted, which means that it’s easy - too easy - to reference the function before it is defined in the file. This harms readability and maintainability. If you find that a function’s definition is large or complex enough that it is interfering with understanding the rest of the file, then perhaps it’s time to extract it to its own module! Don’t forget to name the expression - anonymous functions can make it harder to locate the problem in an Error’s call stack. ([Discussion](https://github.com/airbnb/javascript/issues/794)) + > Why? Function declarations are hoisted, which means that it’s easy - too easy - to reference the function before it is defined in the file. This harms readability and maintainability. If you find that a function’s definition is large or complex enough that it is interfering with understanding the rest of the file, then perhaps it’s time to extract it to its own module! Don’t forget to explicitly name the expression, regardless of whether or not the name is inferred from the containing variable (which is often the case in modern browsers or when using compilers such as Babel). This eliminates any assumptions made about the Error's call stack. ([Discussion](https://github.com/airbnb/javascript/issues/794)) ```javascript // bad @@ -647,7 +647,8 @@ Other Style Guides }; // good - const foo = function bar() { + // lexical name distinguished from the variable-referenced invocation(s) + const foo = function uniqueMoreDescriptiveLexicalFoo() { // ... }; ```