简体   繁体   中英

Airbnb's ES6 style guide recommendation about functions

Referring to section 7.1 in the style guide:

// bad
function foo() {
}

// good
const foo = function bar() {
};

I can't seem to understand what exactly is wrong with the first statement ? How is it bad ? (fyi .. I do understand the difference that the first declaration is hoisted and the 'const' do not get hoisted. What I do not understand is being hoisted bad ?

AirBnB has already explained why they consider function declarations hosting a bad thing here :

7.1 Use named function expressions instead of function declarations.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM