Skip to content

Commit 5404c22

Browse files
committed
Merge branch 'master' of https://github.com/javascript-tutorial/en.javascript.info into dragndrop
2 parents e044661 + 9b5c1c9 commit 5404c22

File tree

119 files changed

+2505
-900
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

119 files changed

+2505
-900
lines changed

1-js/01-getting-started/1-intro/article.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,15 @@ So recently a plethora of new languages appeared, which are *transpiled* (conver
105105

106106
Modern tools make the transpilation very fast and transparent, actually allowing developers to code in another language and auto-converting it "under the hood".
107107

108+
The most used are
108109
Examples of such languages:
109110

110111
- [CoffeeScript](http://coffeescript.org/) is a "syntactic sugar" for JavaScript. It introduces shorter syntax, allowing us to write clearer and more precise code. Usually, Ruby devs like it.
111112
- [TypeScript](http://www.typescriptlang.org/) is concentrated on adding "strict data typing" to simplify the development and support of complex systems. It is developed by Microsoft.
112-
- [Dart](https://www.dartlang.org/) is a standalone language that has its own engine that runs in non-browser environments (like mobile apps). It was initially offered by Google as a replacement for JavaScript, but as of now, browsers require it to be transpiled to JavaScript just like the ones above.
113+
- [Flow](http://flow.org/) also adds data typing, but in a different way. Developed by Facebook.
114+
- [Dart](https://www.dartlang.org/) is a standalone language that has its own engine that runs in non-browser environments (like mobile apps), but also can be transpiled to JavaScript. Developed by Google.
113115

114-
There are more. Of course, even if we use one of these languages, we should also know JavaScript to really understand what we're doing.
116+
There are more. Of course, even if we use one of transpiled languages, we should also know JavaScript to really understand what we're doing.
115117

116118
## Summary
117119

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
# Manuals and specifications
3+
4+
This book is a *tutorial*. It aims to help you gradually learn the language. But once you're familiar with the basics, you'll need other sources.
5+
6+
7+
## Specification
8+
9+
**The ECMA-262 specification** contains the most in-depth, detailed and formalized information about JavaScript. It defines the language.
10+
11+
But being that formalized, it's difficult to understand at first. So if you need the most trustworthy source of information about the language details, it's the right place. But it's not for everyday use.
12+
13+
The latest draft is at <https://tc39.es/ecma262/>.
14+
15+
To read about bleeding-edge features, that are not yet widely supported, see proposals at <https://github.com/tc39/proposals>.
16+
17+
Also, if you're in developing for the browser, then there are other specs covered in the [second part](info:browser-environment) of the tutorial.
18+
19+
## Manuals
20+
21+
- **MDN (Mozilla) JavaScript Reference** is a manual with examples and other information. It's great to get in-depth information about individual language functions, methods etc.
22+
23+
One can find it at <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference>.
24+
25+
Although, it's often best to use an internet search instead. Just use "MDN [term]" in the query, e.g. <https://google.com/search?q=MDN+parseInt> to search for `parseInt` function.
26+
27+
28+
- **MSDN** – Microsoft manual with a lot of information, including JavaScript (often referrerd to as JScript). If one needs something specific to Internet Explorer, better go there: <http://msdn.microsoft.com/>.
29+
30+
Also, we can use an internet search with phrases such as "RegExp MSDN" or "RegExp MSDN jscript".
31+
32+
## Feature support
33+
34+
JavaScript is a developing language, new features get added regularly.
35+
36+
To see their support among browser-based and other engines, see:
37+
38+
- <http://caniuse.com> - per-feature tables of support, e.g. to see which engines support modern cryptography functions: <http://caniuse.com/#feat=cryptography>.
39+
- <https://kangax.github.io/compat-table> - a table with language features and engines that support those or don't support.
40+
41+
All these resources are useful in real-life development, as they contain valuable information about language details, their support etc.
42+
43+
Please remember them (or this page) for the cases when you need in-depth information about a particular feature.

1-js/01-getting-started/2-code-editors/article.md renamed to 1-js/01-getting-started/3-code-editors/article.md

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@ An IDE loads the project (which can be many files), allows navigation between fi
1212

1313
If you haven't selected an IDE yet, consider the following options:
1414

15-
- [WebStorm](http://www.jetbrains.com/webstorm/) for frontend development. The same company offers other editors for other languages (paid).
16-
- [Netbeans](http://netbeans.org/) (free).
15+
- [Visual Studio Code](https://code.visualstudio.com/) (cross-platform, free).
16+
- [WebStorm](http://www.jetbrains.com/webstorm/) (cross-platform, paid).
1717

18-
All of these IDEs are cross-platform.
18+
For Windows, there's also "Visual Studio", not to be confused with "Visual Studio Code". "Visual Studio" is a paid and mighty Windows-only editor, well-suited for the .NET platform. It's also good at JavaScript. There's also a free version [Visual Studio Community](https://www.visualstudio.com/vs/community/).
1919

20-
For Windows, there's also "Visual Studio", not to be confused with "Visual Studio Code." "Visual Studio" is a paid and mighty Windows-only editor, well-suited for the .NET platform. A free version of it is called [Visual Studio Community](https://www.visualstudio.com/vs/community/).
21-
22-
Many IDEs are paid but have a trial period. Their cost is usually negligible compared to a qualified developer's salary, so just choose the best one for you.
20+
Many IDEs are paid, but have a trial period. Their cost is usually negligible compared to a qualified developer's salary, so just choose the best one for you.
2321

2422
## Lightweight editors
2523

@@ -33,21 +31,11 @@ In practice, lightweight editors may have a lot of plugins including directory-l
3331

3432
The following options deserve your attention:
3533

36-
- [Visual Studio Code](https://code.visualstudio.com/) (cross-platform, free) also has many IDE-like features.
3734
- [Atom](https://atom.io/) (cross-platform, free).
3835
- [Sublime Text](http://www.sublimetext.com) (cross-platform, shareware).
3936
- [Notepad++](https://notepad-plus-plus.org/) (Windows, free).
4037
- [Vim](http://www.vim.org/) and [Emacs](https://www.gnu.org/software/emacs/) are also cool if you know how to use them.
4138

42-
## My favorites
43-
44-
The personal preference of the author is to have both an IDE for projects and a lightweight editor for quick and easy file editing.
45-
46-
I'm using:
47-
48-
- As an IDE for JS -- [WebStorm](http://www.jetbrains.com/webstorm/) (I switch to one of the other JetBrains offerings when using other languages)
49-
- As a lightweight editor -- [Sublime Text](http://www.sublimetext.com) or [Atom](https://atom.io/).
50-
5139
## Let's not argue
5240

5341
The editors in the lists above are those that either I or my friends whom I consider good developers have been using for a long time and are happy with.

1-js/02-first-steps/03-strict-mode/article.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,17 @@ For the future, when you use a browser console to test features, please note tha
5353
5454
Sometimes, when `use strict` makes a difference, you'll get incorrect results.
5555
56-
Even if we press `key:Shift+Enter` to input multiple lines, and put `use strict` on top, it doesn't work. That's because of how the console executes the code internally.
56+
You can try to press `key:Shift+Enter` to input multiple lines, and put `use strict` on top, like this:
5757
58-
The reliable way to ensure `use strict` would be to input the code into console like this:
58+
```js
59+
'use strict'; <Shift+Enter for a newline>
60+
// ...your code
61+
<Enter to run>
62+
```
63+
64+
It works in most browsers, namely Firefox and Chrome.
65+
66+
If it doesn't, the most reliable way to ensure `use strict` would be to input the code into console like this:
5967

6068
```js
6169
(function() {

1-js/03-code-quality/02-coding-style/article.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,8 @@ Here's an example of an `.eslintrc` file:
304304
},
305305
"rules": {
306306
"no-console": 0,
307-
},
308-
"indent": 2
307+
"indent": ["warning", 2]
308+
}
309309
}
310310
```
311311

1-js/03-code-quality/04-ninja-code/article.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ Instead, reuse existing names. Just write new values into them.
137137

138138
In a function try to use only variables passed as parameters.
139139

140-
That would make it really hard to identify what's exactly in the variable *now*. And also where it comes from. A person with weak intuition would have to analyze the code line-by-line and track the changes through every code branch.
140+
That would make it really hard to identify what's exactly in the variable *now*. And also where it comes from. The purpose is to develop the intuition and memory of a person reading the code. A person with weak intuition would have to analyze the code line-by-line and track the changes through every code branch.
141141

142142
**An advanced variant of the approach is to covertly (!) replace the value with something alike in the middle of a loop or a function.**
143143

@@ -155,7 +155,7 @@ function ninjaFunction(elem) {
155155

156156
A fellow programmer who wants to work with `elem` in the second half of the function will be surprised... Only during the debugging, after examining the code they will find out that they're working with a clone!
157157

158-
Seen in code regularly. Deadly effective even against an experienced ninja.
158+
Seen in code regularly. Deadly effective even against an experienced ninja.
159159

160160
## Underscores for fun
161161

@@ -169,8 +169,7 @@ A smart ninja puts underscores at one spot of code and evades them at other plac
169169

170170
Let everyone see how magnificent your entities are! Names like `superElement`, `megaFrame` and `niceItem` will definitely enlighten a reader.
171171

172-
Indeed, from one hand, something is written: `super..`, `mega..`, `nice..` But from the other hand -- that brings no details. A reader may decide to look for a hidden meaning and meditate for an hour or two.
173-
172+
Indeed, from one hand, something is written: `super..`, `mega..`, `nice..` But from the other hand -- that brings no details. A reader may decide to look for a hidden meaning and meditate for an hour or two of their paid working time.
174173

175174

176175
## Overlap outer variables
@@ -180,7 +179,7 @@ When in the light, can't see anything in the darkness.<br>
180179
When in the darkness, can see everything in the light.
181180
```
182181

183-
Use same names for variables inside and outside a function. As simple. No efforts required.
182+
Use same names for variables inside and outside a function. As simple. No efforts to invent new names.
184183

185184
```js
186185
let *!*user*/!* = authenticateUser();

1-js/04-object-basics/02-garbage-collection/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,6 @@ A general book "The Garbage Collection Handbook: The Art of Automatic Memory Man
207207

208208
If you are familiar with low-level programming, the more detailed information about V8 garbage collector is in the article [A tour of V8: Garbage Collection](http://jayconrod.com/posts/55/a-tour-of-v8-garbage-collection).
209209

210-
[V8 blog](http://v8project.blogspot.com/) also publishes articles about changes in memory management from time to time. Naturally, to learn the garbage collection, you'd better prepare by learning about V8 internals in general and read the blog of [Vyacheslav Egorov](http://mrale.ph) who worked as one of V8 engineers. I'm saying: "V8", because it is best covered with articles in the internet. For other engines, many approaches are similar, but garbage collection differs in many aspects.
210+
[V8 blog](https://v8.dev/) also publishes articles about changes in memory management from time to time. Naturally, to learn the garbage collection, you'd better prepare by learning about V8 internals in general and read the blog of [Vyacheslav Egorov](http://mrale.ph) who worked as one of V8 engineers. I'm saying: "V8", because it is best covered with articles in the internet. For other engines, many approaches are similar, but garbage collection differs in many aspects.
211211

212212
In-depth knowledge of engines is good when you need low-level optimizations. It would be wise to plan that as the next step after you're familiar with the language.

1-js/04-object-basics/03-symbol/article.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ A value of this type can be created using `Symbol()`:
1616
let id = Symbol();
1717
```
1818

19-
We can also give symbol a description (also called a symbol name), mostly useful for debugging purposes:
19+
Upon creation, we can give symbol a description (also called a symbol name), mostly useful for debugging purposes:
2020

2121
```js run
2222
// id is a symbol with the description "id"
@@ -74,7 +74,9 @@ alert(id.description); // id
7474

7575
Symbols allow us to create "hidden" properties of an object, that no other part of code can occasionally access or overwrite.
7676

77-
For instance, if we want to store an "identifier" for the object `user`, we can use a symbol as a key for it:
77+
For instance, if we're working with `user` objects, that belong to a third-party code and don't have any `id` field. We'd like to add identifiers to them.
78+
79+
Let's use a symbol key for it:
7880

7981
```js run
8082
let user = { name: "John" };
@@ -86,9 +88,9 @@ alert( user[id] ); // we can access the data using the symbol as the key
8688

8789
What's the benefit of using `Symbol("id")` over a string `"id"`?
8890

89-
Let's make the example a bit deeper to see that.
91+
As `user` objects belongs to another code, and that code also works with them, we shouldn't just add any fields to it. That's unsafe. But a symbol cannot be accessed occasionally, the third-party code probably won't even see it, so it's probably all right to do.
9092

91-
Imagine that another script wants to have its own "id" property inside `user`, for its own purposes. That may be another JavaScript library, so the scripts are completely unaware of each other.
93+
Also, imagine that another script wants to have its own identifier inside `user`, for its own purposes. That may be another JavaScript library, so that the scripts are completely unaware of each other.
9294

9395
Then that script can create its own `Symbol("id")`, like this:
9496

@@ -99,9 +101,9 @@ let id = Symbol("id");
99101
user[id] = "Their id value";
100102
```
101103

102-
There will be no conflict, because symbols are always different, even if they have the same name.
104+
There will be no conflict between our and their identifiers, because symbols are always different, even if they have the same name.
103105

104-
Now note that if we used a string `"id"` instead of a symbol for the same purpose, then there *would* be a conflict:
106+
...But if we used a string `"id"` instead of a symbol for the same purpose, then there *would* be a conflict:
105107

106108
```js run
107109
let user = { name: "John" };
@@ -117,7 +119,7 @@ user.id = "Their id value"
117119

118120
### Symbols in a literal
119121

120-
If we want to use a symbol in an object literal, we need square brackets.
122+
If we want to use a symbol in an object literal `{...}`, we need square brackets around it.
121123

122124
Like this:
123125

@@ -155,7 +157,7 @@ for (let key in user) alert(key); // name, age (no symbols)
155157
alert( "Direct: " + user[id] );
156158
```
157159

158-
That's a part of the general "hiding" concept. If another script or a library loops over our object, it won't unexpectedly access a symbolic property.
160+
`Object.keys(user)` also ignores them. That's a part of the general "hiding symbolic properties" principle. If another script or a library loops over our object, it won't unexpectedly access a symbolic property.
159161

160162
In contrast, [Object.assign](mdn:js/Object/assign) copies both string and symbol properties:
161163

@@ -190,13 +192,13 @@ alert( obj[0] ); // test (same property)
190192

191193
## Global symbols
192194

193-
As we've seen, usually all symbols are different, even if they have the same names. But sometimes we want same-named symbols to be same entities.
195+
As we've seen, usually all symbols are different, even if they have the same name. But sometimes we want same-named symbols to be same entities.
194196

195197
For instance, different parts of our application want to access symbol `"id"` meaning exactly the same property.
196198

197199
To achieve that, there exists a *global symbol registry*. We can create symbols in it and access them later, and it guarantees that repeated accesses by the same name return exactly the same symbol.
198200

199-
In order to create or read a symbol in the registry, use `Symbol.for(key)`.
201+
In order to read (create if absent) a symbol from the registry, use `Symbol.for(key)`.
200202

201203
That call checks the global registry, and if there's a symbol described as `key`, then returns it, otherwise creates a new symbol `Symbol(key)` and stores it in the registry by the given `key`.
202204

@@ -206,7 +208,7 @@ For instance:
206208
// read from the global registry
207209
let id = Symbol.for("id"); // if the symbol did not exist, it is created
208210

209-
// read it again
211+
// read it again (maybe from another part of the code)
210212
let idAgain = Symbol.for("id");
211213

212214
// the same symbol
@@ -266,14 +268,14 @@ Other symbols will also become familiar when we study the corresponding language
266268

267269
`Symbol` is a primitive type for unique identifiers.
268270

269-
Symbols are created with `Symbol()` call with an optional description.
271+
Symbols are created with `Symbol()` call with an optional description (name).
270272

271-
Symbols are always different values, even if they have the same name. If we want same-named symbols to be equal, then we should use the global registry: `Symbol.for(key)` returns (creates if needed) a global symbol with `key` as the name. Multiple calls of `Symbol.for` return exactly the same symbol.
273+
Symbols are always different values, even if they have the same name. If we want same-named symbols to be equal, then we should use the global registry: `Symbol.for(key)` returns (creates if needed) a global symbol with `key` as the name. Multiple calls of `Symbol.for` with the same `key` return exactly the same symbol.
272274

273275
Symbols have two main use cases:
274276

275277
1. "Hidden" object properties.
276-
If we want to add a property into an object that "belongs" to another script or a library, we can create a symbol and use it as a property key. A symbolic property does not appear in `for..in`, so it won't be occasionally listed. Also it won't be accessed directly, because another script does not have our symbol, so it will not occasionally intervene into its actions.
278+
If we want to add a property into an object that "belongs" to another script or a library, we can create a symbol and use it as a property key. A symbolic property does not appear in `for..in`, so it won't be occasionally processed together with other properties. Also it won't be accessed directly, because another script does not have our symbol. So the property will be protected from occasional use or overwrite.
277279

278280
So we can "covertly" hide something into objects that we need, but others should not see, using symbolic properties.
279281

1-js/04-object-basics/04-object-methods/2-check-syntax/solution.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ The error message in most browsers does not give understanding what went wrong.
1515

1616
**The error appears because a semicolon is missing after `user = {...}`.**
1717

18-
JavaScript does not assume a semicolon before a bracket `(user.go)()`, so it reads the code like:
18+
JavaScript does not auto-insert a semicolon before a bracket `(user.go)()`, so it reads the code like:
1919

2020
```js no-beautify
2121
let user = { go:... }(user.go)()
2222
```
2323

24-
Then we can also see that such a joint expression is syntactically a call of the object `{ go: ... }` as a function with the argument `(user.go)`. And that also happens on the same line with `let user`, so the `user` object has not yet even been defined, hence the error.
24+
Then we can also see that such a joint expression is syntactically a call of the object `{ go: ... }` as a function with the argument `(user.go)`. And that also happens on the same line with `let user`, so the `user` object has not yet even been defined, hence the error.
2525

2626
If we insert the semicolon, all is fine:
2727

@@ -35,9 +35,3 @@ let user = {
3535
```
3636

3737
Please note that brackets around `(user.go)` do nothing here. Usually they setup the order of operations, but here the dot `.` works first anyway, so there's no effect. Only the semicolon thing matters.
38-
39-
40-
41-
42-
43-

1-js/04-object-basics/04-object-methods/4-object-property-this/solution.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ let user = makeUser();
1414
alert( user.ref.name ); // Error: Cannot read property 'name' of undefined
1515
```
1616

17-
That's because rules that set `this` do not look at object literals.
17+
That's because rules that set `this` do not look at object definition. Only the moment of call matters.
1818

19-
Here the value of `this` inside `makeUser()` is `undefined`, because it is called as a function, not as a method.
19+
Here the value of `this` inside `makeUser()` is `undefined`, because it is called as a function, not as a method with "dot" syntax.
2020

21-
And the object literal itself has no effect on `this`. The value of `this` is one for the whole function, code blocks and object literals do not affect it.
21+
The value of `this` is one for the whole function, code blocks and object literals do not affect it.
2222

2323
So `ref: this` actually takes current `this` of the function.
2424

@@ -42,5 +42,3 @@ alert( user.ref().name ); // John
4242
```
4343

4444
Now it works, because `user.ref()` is a method. And the value of `this` is set to the object before dot `.`.
45-
46-

0 commit comments

Comments
 (0)