minor
This commit is contained in:
parent
b9714f12c8
commit
2035e46305
2 changed files with 5 additions and 5 deletions
|
@ -101,7 +101,7 @@ showMessage();
|
|||
alert( userName ); // *!*Bob*/!*, the value was modified by the function
|
||||
```
|
||||
|
||||
The outer variable is only used if there's no local one. So an occasional modification may happen if we forget `let`.
|
||||
The outer variable is only used if there's no local one.
|
||||
|
||||
If a same-named variable is declared inside the function then it *shadows* the outer one. For instance, in the code below the function uses the local `userName`. The outer one is ignored:
|
||||
|
||||
|
@ -128,7 +128,7 @@ Variables declared outside of any function, such as the outer `userName` in the
|
|||
|
||||
Global variables are visible from any function (unless shadowed by locals).
|
||||
|
||||
Usually, a function declares all variables specific to its task. Global variables only store project-level data, and it's important that these variables are accessible from anywhere. Modern code has few or no globals. Most variables reside in their functions.
|
||||
It's a good practice to minimize the use of global variables. Modern code has few or no globals. Most variables reside in their functions. Sometimes though, they can be useful to store project-level data.
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
@ -376,7 +376,7 @@ A 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).
|
||||
- `checkPermission` -- would be bad if it 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 have a firm understanding of what a prefix means, what a prefixed function can and cannot do. All same-prefixed functions should obey the rules. And the team should share the knowledge.
|
||||
These examples assume common meanings of prefixes. You and your team are free to agree on other meanings, but usually they're not much different. In any case, you should have a firm understanding of what a prefix means, what a prefixed function can and cannot do. All same-prefixed functions should obey the rules. And the team should share the knowledge.
|
||||
```
|
||||
|
||||
```smart header="Ultrashort function names"
|
||||
|
|
|
@ -61,7 +61,7 @@ let reg = new RegExp("\d\.\d");
|
|||
alert( "Chapter 5.1".match(reg) ); // null
|
||||
```
|
||||
|
||||
It worked with `pattern:/\d\.\d/`, but with `new RegExp("\d\.\d")` it doesn't, why?
|
||||
The search worked with `pattern:/\d\.\d/`, but with `new RegExp("\d\.\d")` it doesn't work, why?
|
||||
|
||||
The reason is that backslashes are "consumed" by a string. Remember, regular strings have their own special characters like `\n`, and a backslash is used for escaping.
|
||||
|
||||
|
@ -77,7 +77,7 @@ The quotes "consume" backslashes and interpret them, for instance:
|
|||
- `\u1234` -- becomes the Unicode character with such code,
|
||||
- ...And when there's no special meaning: like `\d` or `\z`, then the backslash is simply removed.
|
||||
|
||||
So the call to `new RegExp` gets a string without backslashes. That's why it doesn't work!
|
||||
So the call to `new RegExp` gets a string without backslashes. That's why the search doesn't work!
|
||||
|
||||
To fix it, we need to double backslashes, because quotes turn `\\` into `\`:
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue