Merge branch 'master' into patch-1
This commit is contained in:
commit
57cc857aab
3 changed files with 14 additions and 16 deletions
|
@ -45,7 +45,7 @@ The Dao hides in wordlessness. Only the Dao is well begun and well
|
||||||
completed.
|
completed.
|
||||||
```
|
```
|
||||||
|
|
||||||
Another way to code faster (and much worse!) is to use single-letter variable names everywhere. Like `a`, `b` or `c`.
|
Another way to code faster is to use single-letter variable names everywhere. Like `a`, `b` or `c`.
|
||||||
|
|
||||||
A short variable disappears in the code like a real ninja in the forest. No one will be able to find it using "search" of the editor. And even if someone does, he won't be able to "decipher" what the name `a` or `b` means.
|
A short variable disappears in the code like a real ninja in the forest. No one will be able to find it using "search" of the editor. And even if someone does, he won't be able to "decipher" what the name `a` or `b` means.
|
||||||
|
|
||||||
|
@ -83,17 +83,19 @@ While choosing a name try to use the most abstract word. Like `obj`, `data`, `va
|
||||||
|
|
||||||
- **Name a variable by its type: `str`, `num`...**
|
- **Name a variable by its type: `str`, `num`...**
|
||||||
|
|
||||||
Give them a try. A young ninja may wonder -- do such names make the code worse? Actually, yes!
|
Give them a try. A young initiate may wonder -- are such names really useful for a ninja? Indeed, they are!
|
||||||
|
|
||||||
Sure, the variable name still means something. It says what's inside the variable: a string, a number or something else. But when an outsider tries to understand the code, he'll be surprised to see that there's actually no information at all!
|
Sure, the variable name still means something. It says what's inside the variable: a string, a number or something else. But when an outsider tries to understand the code, he'll be surprised to see that there's actually no information at all! And will ultimately fail to alter your well-thought code.
|
||||||
|
|
||||||
Indeed, the value type is easy to find out by debugging. But what's the meaning of the variable? Which string/number does it store? There's just no way to figure out without a good meditation!
|
The value type is easy to find out by debugging. But what's the meaning of the variable? Which string/number does it store?
|
||||||
|
|
||||||
|
There's just no way to figure out without a good meditation!
|
||||||
|
|
||||||
- **...But what if there are no more such names?** Just add a number: `data1, item2, elem5`...
|
- **...But what if there are no more such names?** Just add a number: `data1, item2, elem5`...
|
||||||
|
|
||||||
## Attention test
|
## Attention test
|
||||||
|
|
||||||
Only a truly attentive programmer should be able to understand the code. But how to check that?
|
Only a truly attentive programmer should be able to understand your code. But how to check that?
|
||||||
|
|
||||||
**One of the ways -- use similar variable names, like `date` and `data`.**
|
**One of the ways -- use similar variable names, like `date` and `data`.**
|
||||||
|
|
||||||
|
|
|
@ -411,8 +411,8 @@ So, "49" is an integer property name, because when it's transformed to an intege
|
||||||
```js run
|
```js run
|
||||||
// Math.trunc is a built-in function that removes the decimal part
|
// Math.trunc is a built-in function that removes the decimal part
|
||||||
alert( String(Math.trunc(Number("49"))) ); // "49", same, integer property
|
alert( String(Math.trunc(Number("49"))) ); // "49", same, integer property
|
||||||
alert( String(Math.trunc(Number("+49"))) ); // "49", not same ⇒ not integer property
|
alert( String(Math.trunc(Number("+49"))) ); // "49", not same "+49" ⇒ not integer property
|
||||||
alert( String(Math.trunc(Number("1.2"))) ); // "1", not same ⇒ not integer property
|
alert( String(Math.trunc(Number("1.2"))) ); // "1", not same "1.2" ⇒ not integer property
|
||||||
```
|
```
|
||||||
````
|
````
|
||||||
|
|
||||||
|
|
|
@ -128,28 +128,24 @@ For that we should use another event -- `onbeforeunload`.
|
||||||
|
|
||||||
## window.onbeforeunload [#window.onbeforeunload]
|
## window.onbeforeunload [#window.onbeforeunload]
|
||||||
|
|
||||||
If a visitor initiated leaving the page or tries to close the window, the `beforeunload` handler can ask for additional confirmation.
|
If a visitor initiated navigation away from the page or tries to close the window, the `beforeunload` handler asks for additional confirmation.
|
||||||
|
|
||||||
It needs to return the string with the question. The browser will show it.
|
It may return a string with the question. Historically browsers used to show it, but as of now only some of them do. That's because certain webmasters abused this event handler, to protect the visitor from potentially misleading and hackish messages.
|
||||||
|
|
||||||
For instance:
|
You can try it by running this code and then reloading the page.
|
||||||
|
|
||||||
```js
|
```js run
|
||||||
window.onbeforeunload = function() {
|
window.onbeforeunload = function() {
|
||||||
return "There are unsaved changes. Leave now?";
|
return "There are unsaved changes. Leave now?";
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
```online
|
```online
|
||||||
Click on the button in `<iframe>` below to set the handler, and then click the link to see it in action:
|
Or you can click on the button in `<iframe>` below to set the handler, and then click the link:
|
||||||
|
|
||||||
[iframe src="window-onbeforeunload" border="1" height="80" link edit]
|
[iframe src="window-onbeforeunload" border="1" height="80" link edit]
|
||||||
```
|
```
|
||||||
|
|
||||||
```warn header="Some browsers ignore the text and show their own message instead"
|
|
||||||
Some browsers like Chrome and Firefox ignore the string and shows its own message instead. That's for sheer safety, to protect the user from potentially misleading and hackish messages.
|
|
||||||
```
|
|
||||||
|
|
||||||
## readyState
|
## readyState
|
||||||
|
|
||||||
What happens if we set the `DOMContentLoaded` handler after the document is loaded?
|
What happens if we set the `DOMContentLoaded` handler after the document is loaded?
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue