Skip to content

Commit

Permalink
feat: Add Why you should use semicolons. Close #112
Browse files Browse the repository at this point in the history
  • Loading branch information
denysdovhan committed Feb 6, 2021
1 parent 81316dc commit 7488b34
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ Currently, there are these translations of **wtfjs**:
- [An infinite timeout](#an-infinite-timeout)
- [Double dot](#double-dot)
- [Extra Newness](#extra-newness)
- [Why you should use semicolons](#why-you-should-use-semicolons)
- [📚 Other resources](#-other-resources)
- [🎓 License](#-license)

Expand Down Expand Up @@ -1969,6 +1970,38 @@ foo.val // -> ':D'

- [Class Extends Function: Extra Newness](https://github.com/denysdovhan/wtfjs/issues/78)

## Why you should use semicolons

Writing some standard JavaScript… and then BOOM!

This comment has been minimized.

Copy link
@qgustavor

qgustavor Jun 22, 2021

Why the "standard JavaScript" wording? It's way too similar to the JavaScript Standard Style which avoids using semicolons but explicitly says would should use semicolons when starting lines with [ and lists other cases where it's needed. Also, says that clever short-hands, which are the main reason of having to use semicolons in this way, are discouraged.

I like standard because it makes programmers think about the actual JavaScript issue: automatic semicolon insertion. Teaching "you should use semicolons" for me is just a workaround for that and which may fail if not learned properly. If someone thinks "I will just use semicolons like in other languages" then it will lead to some pitfalls like adding a line break after a return: works in PHP, works in C, not in JavaScript.

At least improve the wording. At best, change this section to "JavaScript automatically adds semicolons, right?" and then show examples where it don't adds semicolons where it was expected and examples where it adds where it wasn't expected.

This comment has been minimized.

Copy link
@qgustavor

```js
class SomeClass {
['array'] = []
['string'] = 'str'
}

(new SomeClass()).array // -> 'str'
```

What the …?

### 💡 Explanation:

Once again, this is all thanks to the Automatic Semicolon Insertion.

An example above is basically the same as:

```js
class SomeClass {
['array'] = ([]['string'] = 'str');
}
```

You basically assign a string `str` into an `array` property.

- [An original tween with an example](https://twitter.com/SeaRyanC/status/1148726605222535168) by Ryan Cavanaugh
- [TC39 meeting when they debated about it](https://github.com/tc39/notes/blob/master/meetings/2017-09/sept-26.md)

# 📚 Other resources

- [wtfjs.com](http://wtfjs.com/) — a collection of those very special irregularities, inconsistencies and just plain painfully unintuitive moments for the language of the web.
Expand Down

0 comments on commit 7488b34

Please sign in to comment.