4 ms·
Regarding the "no semicolon" stuff, is there actually any way to write this type of formatting list.map(func1) .filter(func2) .map(func3)
by kuschku 11y ago
Regarding the "no semicolon" stuff, is there actually any way to write this type of formatting
list.map(func1)
.filter(func2)
.map(func3)
.reduce(func4);
in JavaScript? As far as I know, the semicolon rules would automatically terminate the statement after map(func1).
EDIT: Ah, thanks – the things I read never mentioned JS doing lookahead during ASI. This makes it a lot nicer, tbh.
- dagw 11y agoThat's fine in JavaScript and will work as expected.
- vkjv 11y agoIt depends on the engine, but most, when parsing, will look-ahead and see that `.` in `.filter(func2)` is an invalid start to an expression and assume it is part of the previous line.
- gberger 11y agoNo, ASI (automatic semicolon insertion) does not insert a semicolon on the first line (or the 2nd or 3rd) in this case. The code works as expected.
- nvader 11y agoIn most cases[0], Js "looks" at the next line to see if it could continue the current statement, in which case no semicolon is inserted. [0] Not true for "restricted productions", which include naked returns, and some other "special" cases.
- kuschku 11y agoWhat do you mean? Would return list.map(func1) .filter(func2); work?
- WorldMaker 11y agoYes, but this return list.map(func1) .filter(func2); has never worked in the history of JS because JS terminates the return at the newline and returns undefined.
- deleted 11y ago[deleted]
- Sacho 11y agoThat's valid javascript, and it would produce what you expect, with or without the ending semicolon. Here is an example: http://jsbin.com/mokipigeqa/edit?html,js,console http://jsbin.com/mokipigeqa/edit?html,js,console Here are the ASI-relevant parts of the ES5 standard: http://es5.github.io/#x7.9 http://es5.github.io/#x7.9 In JavaScript, LineTerminators are used as a separator between tokens, just like other whitespace is. Therefore: list .map() .filter() is equivalent to list .map() .filter()
- madeofpalk 11y agoThat pattern is valid JS, and fairly common, especially with 'functional programming' utility libraries such as lodash.