Callback functions are hoisted in JavaScript

When should I use arrow functions in ECMAScript 6?

According to the proposal, the arrows were aimed at "addressing and solving several common weaknesses in traditional dressing". They wanted to improve things by lexically binding and offering terse syntax.


  • One cannot consistently bind lexically
  • The arrow function syntax is tricky and ambiguous

As such, arrow functions create confusion and error and should be excluded from a JavaScript programmer's vocabulary that is only replaced by.

Regarding lexical

is problematic:

Arrow functions are designed to solve the problem of having to access a property of within a callback. There are already several ways to do this: you can assign to a variable, use it, or use the third argument available for the aggregate methods. Still, arrows seem to be the easiest workaround, so the method could be revised as follows:

However, consider whether the code uses a library like jQuery whose methods specifically bind. Now there are two values ​​for:

We need to use that to dynamically bind. We cannot use an arrow function here.

Dealing with multiple values ​​can also be confusing as it is difficult to see which one an author was talking about:

Did the author really want to call? Or did he forget to bind and call out? If we change the handler to an arrow function, we will also wonder if the author wanted the dynamic one, but still selected an arrow because it fits on a line:

One could say, "Is it extraordinary that arrows are sometimes the wrong function? If we rarely need dynamic values, it is still okay to use arrows most of the time."

But ask yourself, "Would it be 'worth' debugging code and finding that the result of an error was caused by an 'edge case'?" 100% of the time.

There is a better way: Always use (so that it can always be dynamically linked) and always reference via a variable. Variables are lexical and take many names. When you assign a variable, your intentions become clear:

It also provides always assigning to a variable (even if there is a single function or no other function) ensures that one's intentions remain clear even after the code has changed.

Even dynamic things are hardly exceptional. jQuery is used on over 50 million websites (as of February 2016). Here are other APIs that bind dynamically:

  • Mocha (~ 120k downloads yesterday) provides methods for its tests via.
  • Grunt (~ 63k downloads yesterday) reveals methods for build tasks over.
  • Backbone (~ 22,000 downloads yesterday) defines methods for accessing.
  • Event APIs (like the DOMs) reference a with.
  • Patched or extended prototype APIs reference instances with.

(Statistics via and

You may already need dynamic bindings.

A lexical is sometimes expected, but sometimes not. just as a dynamic one is sometimes expected, but sometimes not. Fortunately, there is a better way that always produces and communicates the expected bond.

Regarding the concise syntax

Arrow functions could provide a "shorter syntactic form" for functions. But will you make these shorter functions more successful?

Is "Easier to Read" than? Maybe it's because it's more likely to produce a single, short line of code. After Dyson The influence of reading speed and line length on screen reading effectiveness ,

A medium line length (55 characters per line) seems to support effective reading at normal and fast speeds. This led to the highest level of understanding. . .

Similar reasons are given for the conditional (ternary) operator and for single-line statements.

However, you are really writing advertised the simple math functions in the proposal? My domains are not mathematical, which is why my subroutines are rarely that elegant. Rather, it is often found that arrow functions remove a column delimitation and break into another line due to the editor or the format template, whereby the "readability" according to Dyson's definition is canceled.

One might ask, "How about using only the short version for short functions when you can?" But now a style rule contradicts a language restriction: "Try to use the shortest possible function notation, taking into account that sometimes only the longest notation binds as expected." Such amalgamation makes arrows particularly vulnerable to abuse.

There are numerous problems with arrow function syntax:

Both functions are syntactically valid. But isn't in the body of, it's just a poorly indented top-level statement.

When expanding to block form, there is no longer any implicit that one could forget to restore. But the expression could only have a side effect, so who knows if an explicit one will be necessary in the future?

What can be thought of as a residual parameter can be analyzed as a spread operator:

The assignment can be confused with the standard arguments:

Blocks look like objects:

What does that mean?

Did the author want to create a no-op or a function that returns an empty object? (With that in mind, should we ever post-set? Should we limit ourselves to just the expression syntax? That would further reduce the frequency of the arrows.)

Looks like And:

To call an arrow function expression immediately, must be placed outside and inside. This is valid and may be by design.

Although if you write with the intention of writing an instantly called function expression, nothing happens.

It is difficult to argue that arrow functions are "more understandable" given the above cases. One does learn could any special rules required to use this syntax. Is it really worth it?

Exceptionally, the syntax of is generalized. Using only means that the language itself prevents you from writing confusing code. To write procedures that should be syntactically understood in all cases, I choose.

To a guideline

You are requesting a policy that must be "clear" and "consistent". The use of arrow functions ultimately leads to syntactically valid, logically invalid code, whereby both function forms are meaningful and arbitrarily intertwined. Therefore I offer the following:

Guide to function notation in ES6:

  • Always create procedures with.
  • Always assign a variable. Don't use.