When should I use Arrow functions in ECMAScript 6?

后端 未结 9 1803
滥情空心
滥情空心 2020-11-21 05:53

The question is directed at people who have thought about code style in the context of the upcoming ECMAScript 6 (Harmony) and who have already worked with the language.

9条回答
  •  半阙折子戏
    2020-11-21 06:05

    I still stand by everything I wrote in my first answer in this thread. However, my opinion on code style has developed since then, so I have a new answer to this question that builds on my last one.

    Regarding lexical this

    In my last answer, I deliberately eschewed an underlying belief I hold about this language, as it was not directly related to the argument I was making. Nonetheless, without this being explicitly stated, I can understand why many people simply balk at my recommendation to not use arrows, when they find arrows so useful.

    My belief is this: we shouldn’t be using this in the first place. Therefore, if a person deliberately avoids using this in his code, then the “lexical this” feature of arrows is of little to no value. Also, under the premise that this is a bad thing, arrow’s treatment of this is less of a “good thing;” instead, it’s more of a form of damage control for another bad language feature.

    I figure that this either does not occur to some people, but even to those to whom it does, they must invariably find themselves working within codebases where this appears a hundred times per file, and a little (or a lot) of damage control is all a reasonable person could hope for. So arrows can be good, in a way, when they make a bad situation better.

    Even if it is easier to write code with this with arrows than without them, the rules for using arrows remain very complex (see: current thread). Thus, guidelines are neither “clear” nor “consistent,” as you’ve requested. Even if programmers know about arrows’ ambiguities, I think they shrug and accept them anyway, because the value of lexical this overshadows them.

    All this is a preface to the following realization: if one does not use this, then the ambiguity about this that arrows normally cause becomes irrelevant. Arrows become more neutral in this context.

    Regarding terse syntax

    When I wrote my first answer, I was of the opinion that even slavish adherence to best practices was a worthwhile price to pay if it meant I could produce more perfect code. But I eventually came to realize that terseness can serve as a form of abstraction that can improve code quality, too — enough so to justify straying from best practices sometimes.

    In other words: dammit, I want one-liner functions, too!

    Regarding a guideline

    With the possibility of this-neutral arrow functions, and terseness being worth pursuit, I offer the following more lenient guideline:

    Guideline for Function Notation in ES6:

    • Don’t use this.
    • Use function declarations for functions you’d call by name (because they’re hoisted).
    • Use arrow functions for callbacks (because they tend to be terser).

提交回复
热议问题