This commit is contained in:
Ilya Kantor 2017-05-10 16:41:49 +03:00
parent 14a882fee0
commit c56693bca8
3 changed files with 9 additions and 8 deletions

View file

@ -49,7 +49,7 @@ while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
} }
``` ```
````smart header="Brackes are not required for a single-line body" ````smart header="Brackets are not required for a single-line body"
If the loop body has a single statement, we can omit the brackets `{…}`: If the loop body has a single statement, we can omit the brackets `{…}`:
```js run ```js run

View file

@ -90,7 +90,7 @@ alert( "I don't know such values" );
``` ```
````smart header="Any expresion can be a `switch/case` argument" ````smart header="Any expresion can be a `switch/case` argument"
Both `switch` and case allow arbitrary expresions. Both `switch` and case allow arbitrary expressions.
For example: For example:
@ -101,14 +101,15 @@ let b = 0;
switch (+a) { switch (+a) {
*!* *!*
case b + 1: case b + 1:
alert( 1 ); alert("this runs, because +a is 1, exactly equals b+1");
break; break;
*/!* */!*
default: default:
alert('no-no, see the code above, it executes'); alert("this doesn't run");
} }
``` ```
Here `+a` gives `1`, that's compared with `b + 1` in `case`, and the corresponding code is executed.
```` ````
## Grouping of "case" ## Grouping of "case"
@ -129,7 +130,7 @@ switch (a) {
case 3: // (*) grouped two cases case 3: // (*) grouped two cases
case 5: case 5:
alert('Wrong!'); alert('Wrong!');
alert('Why don't you take a math class?'); alert("Why don't you take a math class?");
break; break;
*/!* */!*
@ -144,7 +145,7 @@ The ability to "group" cases a side-effect of how `switch/case` works without `b
## Type matters ## Type matters
Let's emphase that the equality check is always strict. The values must be of the same type to match. Let's emphasize that the equality check is always strict. The values must be of the same type to match.
For example, let's consider the code: For example, let's consider the code:
@ -169,4 +170,4 @@ switch (arg) {
1. For `0`, `1`, the first `alert` runs. 1. For `0`, `1`, the first `alert` runs.
2. For `2` the second `alert` runs. 2. For `2` the second `alert` runs.
3. But for `3`, the result of the `prompt` is a string `"3"`, which is not strictly equal `===` to the number `3`. So we've got a dead code in `case 3`! The `default` variant will execite. 3. But for `3`, the result of the `prompt` is a string `"3"`, which is not strictly equal `===` to the number `3`. So we've got a dead code in `case 3`! The `default` variant will execute.

View file

@ -372,7 +372,7 @@ Few examples of breaking this rule:
- `createForm` -- would be bad if it modifies the document, adding a form to it (should only create it and return). - `createForm` -- would be bad if it modifies the document, adding a form to it (should only create it and return).
- `checkPermission` -- would be bad if displays the `access granted/denied` message (should only perform the check and return the result). - `checkPermission` -- would be bad if displays the `access granted/denied` message (should only perform the check and return the result).
These examples assume common meanings of prefixes. What they mean for you is determined by you and your team. Maybe it's pretty normal for your code to behave differently. But you should to have a firm understanding what a prefix means, what a prefixed function can and what it can not do. All same-prefixed functions should obey the rules. And the team should share the knowledge. These examples assume common meanings of prefixes. What they mean for you is determined by you and your team. Maybe it's pretty normal for your code to behave differently. But you should have a firm understanding of what a prefix means, what a prefixed function can and what it cannot do. All same-prefixed functions should obey the rules. And the team should share the knowledge.
``` ```
```smart header="Ultrashort function names" ```smart header="Ultrashort function names"