Skip to content

Commit d3d9a7c

Browse files
committed
Resolve conflicts/Update files
1 parent 21c93cf commit d3d9a7c

File tree

12 files changed

+145
-227
lines changed

12 files changed

+145
-227
lines changed
Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
1-
Primeiro, a variável para o nome do nosso planeta.
1+
## A variável para o nosso planeta
22

33
Isso é simples:
44

55
```js
66
let ourPlanetName = "Earth";
77
```
88

9-
<<<<<<< HEAD
10-
Note que poderíamos usar um nome mais curto, `planet`, mas pode não ser óbvio a que planeta se refere. É bom ser mais detalhado. Pelo menos até a variável isNotTooLong.
11-
=======
12-
Note, we could use a shorter name `planet`, but it might not be obvious what planet it refers to. It's nice to be more verbose. At least until the variable isNotTooLong.
13-
>>>>>>> c56e6a57ac3497aab77128c5bfca13513980709b
9+
Note que poderíamos usar um nome mais curto, `planet`, mas pode não ser óbvio a que planeta se refere. É bom ser mais detalhado. Pelo menos até a variável naoForDemasiadoLonga.
1410

15-
Em segundo lugar, o nome do visitante atual:
11+
## O nome do visitante atual
1612

1713
```js
1814
let currentUserName = "John";
1915
```
2016

21-
Novamente, nós poderíamos encurtar isso para `userName` se tivermos certeza que o usuário é atual.
17+
Novamente, nós poderíamos encurtar isso para `userName` se tivermos a certeza de que o usuário é o atual.
2218

2319
Os editores modernos e o autocomplete facilitam a escrita de nomes longos de variáveis. Não salve neles. Um nome com 3 palavras é o suficiente.
2420

25-
E se o seu editor não possui autocompletar corretamente, obtenha [um novo](/code-editors).
21+
E se o seu editor não possui autocompletar corretamente, obtenha [um novo](/code-editors).

1-js/03-code-quality/06-polyfills/article.md

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,6 @@ A [transpiler](https://en.wikipedia.org/wiki/Source-to-source_compiler) is a spe
2626

2727
E.g. JavaScript before year 2020 didn't have the "nullish coalescing operator" `??`. So, if a visitor uses an outdated browser, it may fail to understand the code like `height = height ?? 100`.
2828

29-
<<<<<<< HEAD
30-
There's a term "polyfill" for scripts that "fill in" the gap and add missing implementations.
31-
32-
Two interesting polyfills are:
33-
- [babel polyfill](https://babeljs.io/docs/usage/polyfill/) that supports a lot, but is big.
34-
- [polyfill.io](http://polyfill.io) service that allows to load/construct polyfills on-demand, depending on the features we need.
35-
36-
So, we need to setup the transpiler and add the polyfill for old engines to support modern features.
37-
38-
If we orient towards modern engines and do not use features except those supported everywhere, then we don't need to use Babel.
39-
=======
4029
A transpiler would analyze our code and rewrite `height ?? 100` into `(height !== undefined && height !== null) ? height : 100`.
4130

4231
```js
@@ -46,7 +35,6 @@ height = height ?? 100;
4635
// after running the transpiler
4736
height = (height !== undefined && height !== null) ? height : 100;
4837
```
49-
>>>>>>> c56e6a57ac3497aab77128c5bfca13513980709b
5038

5139
Now the rewritten code is suitable for older JavaScript engines.
5240

@@ -64,15 +52,6 @@ For example, `Math.trunc(n)` is a function that "cuts off" the decimal part of a
6452

6553
In some (very outdated) JavaScript engines, there's no `Math.trunc`, so such code will fail.
6654

67-
<<<<<<< HEAD
68-
```offline
69-
As you're reading the offline version, examples are not runnable. But they usually work :)
70-
```
71-
72-
[Chrome Canary](https://www.google.com/chrome/browser/canary.html) is good for all examples, but other modern browsers are mostly fine too.
73-
74-
Note that on production we can use Babel to translate the code into suitable for less recent browsers, so there will be no such limitation, the code will run everywhere.
75-
=======
7655
As we're talking about new functions, not syntax changes, there's no need to transpile anything here. We just need to declare the missing function.
7756

7857
A script that updates/adds new functions is called "polyfill". It "fills in" the gap and adds missing implementations.
@@ -111,4 +90,3 @@ Good resources that show the current state of support for various features:
11190

11291
P.S. Google Chrome is usually the most up-to-date with language features, try it if a tutorial demo fails. Most tutorial demos work with any modern browser though.
11392

114-
>>>>>>> c56e6a57ac3497aab77128c5bfca13513980709b

1-js/05-data-types/04-array/article.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# Arrays
1+
# Arrays
22

33
Objects allow you to store keyed collections of values. That's fine.
44

5-
But quite often we find that we need an *ordered collection*, where we have a 1st, a 2nd, a 3rd element and so on. For example, we need that to store a list of something: users, goods, HTML elements etc.
5+
But quite often we find that we need an *ordered collection*, where we have a 1st, a 2nd, a 3rd element and so on. For example, we need that to store a list of something: users, goods, HTML elements etc.
66

77
It is not convenient to use an object here, because it provides no methods to manage the order of elements. We can’t insert a new property “between” the existing ones. Objects are just not meant for such use.
88

9-
There exists a special data structure named `Array`, to store ordered collections.
9+
There exists a special data structure named `Array`, to store ordered collections.
1010

1111
## Declaration
1212

@@ -81,10 +81,10 @@ arr[3](); // hello
8181

8282
````smart header="Trailing comma"
8383
An array, just like an object, may end with a comma:
84-
```js
84+
```js
8585
let fruits = [
86-
"Apple",
87-
"Orange",
86+
"Apple",
87+
"Orange",
8888
"Plum"*!*,*/!*
8989
];
9090
```
@@ -95,7 +95,7 @@ The "trailing comma" style makes it easier to insert/remove items, because all l
9595

9696
## Methods pop/push, shift/unshift
9797

98-
A [queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)) is one of most common uses of an array. In computer science, this means an ordered collection of elements which supports two operations:
98+
A [queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)) is one of the most common uses of an array. In computer science, this means an ordered collection of elements which supports two operations:
9999

100100
- `push` appends an element to the end.
101101
- `shift` get an element from the beginning, advancing the queue, so that the 2nd element becomes the 1st.
@@ -106,7 +106,7 @@ Arrays support both operations.
106106

107107
In practice we need it very often. For example, a queue of messages that need to be shown on-screen.
108108

109-
There's another use case for arrays -- the data structure named [stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)).
109+
There's another use case for arrays -- the data structure named [stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)).
110110

111111
It supports two operations:
112112

@@ -121,7 +121,7 @@ A stack is usually illustrated as a pack of cards: new cards are added to the to
121121

122122
For stacks, the latest pushed item is received first, that's also called LIFO (Last-In-First-Out) principle. For queues, we have FIFO (First-In-First-Out).
123123

124-
Arrays in JavaScript can work both as a queue and as a stack. They allow you to add/remove elements both to/from the beginning or the end.
124+
Arrays in JavaScript can work both as a queue and as a stack. They allow you to add/remove elements both to/from the beginning or the end.
125125

126126
In computer science the data structure that allows this, is called [deque](https://en.wikipedia.org/wiki/Double-ended_queue).
127127

@@ -189,7 +189,7 @@ alert( fruits );
189189

190190
## Internals
191191

192-
An array is a special kind of object. The square brackets used to access a property `arr[0]` actually come from the object syntax. Numbers are used as keys.
192+
An array is a special kind of object. The square brackets used to access a property `arr[0]` actually come from the object syntax. That's essentially the same as `obj[key]`, where `arr` is the object, while numbers are used as keys.
193193

194194
They extend objects providing special methods to work with ordered collections of data and also the `length` property. But at the core it's still an object.
195195

@@ -203,7 +203,7 @@ let fruits = ["Banana"]
203203
let arr = fruits; // copy by reference (two variables reference the same array)
204204

205205
alert( arr === fruits ); // true
206-
206+
207207
arr.push("Pear"); // modify the array by reference
208208

209209
alert( fruits ); // Banana, Pear - 2 items now
@@ -229,7 +229,7 @@ But the engine will see that we're working with the array as with a regular obje
229229

230230
The ways to misuse an array:
231231

232-
- Add a non-numeric property like `arr.test = 5`.
232+
- Add a non-numeric property like `arr.test = 5`.
233233
- Make holes, like: add `arr[0]` and then `arr[1000]` (and nothing between them).
234234
- Fill the array in the reverse order, like `arr[1000]`, `arr[999]` and so on.
235235

@@ -296,7 +296,7 @@ let fruits = ["Apple", "Orange", "Plum"];
296296

297297
// iterates over array elements
298298
for (let fruit of fruits) {
299-
alert( fruit );
299+
alert( fruit );
300300
}
301301
```
302302

@@ -320,7 +320,7 @@ But that's actually a bad idea. There are potential problems with it:
320320

321321
There are so-called "array-like" objects in the browser and in other environments, that *look like arrays*. That is, they have `length` and indexes properties, but they may also have other non-numeric properties and methods, which we usually don't need. The `for..in` loop will list them though. So if we need to work with array-like objects, then these "extra" properties can become a problem.
322322

323-
2. The `for..in` loop is optimized for generic objects, not arrays, and thus is 10-100 times slower. Of course, it's still very fast. The speedup may only matter in bottlenecks or seem irrelevant. But still we should be aware of the difference.
323+
2. The `for..in` loop is optimized for generic objects, not arrays, and thus is 10-100 times slower. Of course, it's still very fast. The speedup may only matter in bottlenecks. But still we should be aware of the difference.
324324

325325
Generally, we shouldn't use `for..in` for arrays.
326326

@@ -338,7 +338,7 @@ fruits[123] = "Apple";
338338
alert( fruits.length ); // 124
339339
```
340340

341-
Note that we usually don't use arrays like that.
341+
Note that we usually don't use arrays like that.
342342

343343
Another interesting thing about the `length` property is that it's writable.
344344

@@ -385,7 +385,7 @@ To evade such surprises, we usually use square brackets, unless we really know w
385385

386386
## Multidimensional arrays
387387

388-
Arrays can have items that are also arrays. We can use it for multidimensional arrays, to store matrices:
388+
Arrays can have items that are also arrays. We can use it for multidimensional arrays, for example to store matrices:
389389

390390
```js run
391391
let matrix = [
@@ -394,7 +394,7 @@ let matrix = [
394394
[7, 8, 9]
395395
];
396396

397-
alert( matrix[1][1] ); // the central element
397+
alert( matrix[1][1] ); // 5, the central element
398398
```
399399

400400
## toString
@@ -492,15 +492,15 @@ Array is a special kind of object, suited to storing and managing ordered data i
492492

493493
The call to `new Array(number)` creates an array with the given length, but without elements.
494494

495-
- The `length` property is the array length or, to be precise, its last numeric index plus one. It is auto-adjusted by array methods.
495+
- The `length` property is the array length or, to be precise, its last numeric index plus one. It is auto-adjusted by array methods.
496496
- If we shorten `length` manually, the array is truncated.
497497

498498
We can use an array as a deque with the following operations:
499499

500500
- `push(...items)` adds `items` to the end.
501501
- `pop()` removes the element from the end and returns it.
502502
- `shift()` removes the element from the beginning and returns it.
503-
- `unshift(...items)` adds items to the beginning.
503+
- `unshift(...items)` adds `items` to the beginning.
504504

505505
To loop over the elements of the array:
506506
- `for (let i=0; i<arr.length; i++)` -- works fastest, old-browser-compatible.

1-js/09-classes/04-private-protected-properties-methods/article.md

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,16 @@ So, all we need to use an object is to know its external interface. We may be co
4848

4949
That was a general introduction.
5050

51-
In JavaScript, there are three types of properties and members:
51+
In JavaScript, there are two types of object fields (properties and methods):
5252

5353
- Public: accessible from anywhere. They comprise the external interface. Until now we were only using public properties and methods.
5454
- Private: accessible only from inside the class. These are for the internal interface.
5555

56-
In many other languages there also exist "protected" fields: accessible only from inside the class and those extending it. They are also useful for the internal interface. They are in a sense more widespread than private ones, because we usually want inheriting classes to gain access to properly do the extension.
56+
In many other languages there also exist "protected" fields: accessible only from inside the class and those extending it (like private, but plus access from inheriting classes). They are also useful for the internal interface. They are in a sense more widespread than private ones, because we usually want inheriting classes to gain access to them.
5757

5858
Protected fields are not implemented in JavaScript on the language level, but in practice they are very convenient, so they are emulated.
5959

60-
In the next step we'll make a coffee machine in JavaScript with all these types of properties. A coffee machine has a lot of details, we won't model them to stay simple (though we could).
60+
Now we'll make a coffee machine in JavaScript with all these types of properties. A coffee machine has a lot of details, we won't model them to stay simple (though we could).
6161

6262
## Protecting "waterAmount"
6363

@@ -87,7 +87,7 @@ Let's change `waterAmount` property to protected to have more control over it. F
8787

8888
**Protected properties are usually prefixed with an underscore `_`.**
8989

90-
That is not enforced on the language level, but there's a convention that such properties and methods should not be accessed from the outside. Most programmers follow it.
90+
That is not enforced on the language level, but there's a well-known convention between programmers that such properties and methods should not be accessed from the outside.
9191

9292
So our property will be called `_waterAmount`:
9393

@@ -166,16 +166,16 @@ class CoffeeMachine {
166166
}
167167

168168
*!*getWaterAmount()*/!* {
169-
return this.waterAmount;
169+
return this._waterAmount;
170170
}
171171
}
172172

173173
new CoffeeMachine().setWaterAmount(100);
174174
```
175175

176-
That looks a bit longer, but functions are more flexible. They can accept multiple arguments (even if we don't need them right now). So, for the future, just in case we need to refactor something, functions are a safer choise.
176+
That looks a bit longer, but functions are more flexible. They can accept multiple arguments (even if we don't need them right now).
177177

178-
Surely, there's a tradeoff. On the other hand, get/set syntax is shorter, so ultimately there's no strict rule, it's up to you to decide.
178+
On the other hand, get/set syntax is shorter, so ultimately there's no strict rule, it's up to you to decide.
179179
````
180180

181181
```smart header="Protected fields are inherited"
@@ -192,9 +192,9 @@ There's a finished JavaScript proposal, almost in the standard, that provides la
192192

193193
Privates should start with `#`. They are only accessible from inside the class.
194194

195-
For instance, here we add a private `#waterLimit` property and extract the water-checking logic into a separate method:
195+
For instance, here's a private `#waterLimit` property and the water-checking private method `#checkWater`:
196196

197-
```js
197+
```js run
198198
class CoffeeMachine {
199199
*!*
200200
#waterLimit = 200;
@@ -207,39 +207,19 @@ class CoffeeMachine {
207207
}
208208
*/!*
209209

210-
<<<<<<< HEAD
211-
_waterAmount = 0;
212-
213-
set waterAmount(value) {
214-
*!*
215-
this.#checkWater(value);
216-
*/!*
217-
this._waterAmount = value;
218-
}
219-
220-
get waterAmount() {
221-
return this.waterAmount;
222-
=======
223210
setWaterAmount(value) {
224211
this.#waterLimit = this.#fixWaterAmount(value);
225-
>>>>>>> c56e6a57ac3497aab77128c5bfca13513980709b
226212
}
227213

228214
}
229215

230216
let coffeeMachine = new CoffeeMachine();
231217

232218
*!*
233-
<<<<<<< HEAD
234-
coffeeMachine.#checkWater(); // Error
235-
=======
236219
// can't access privates from outside of the class
237220
coffeeMachine.#fixWaterAmount(123); // Error
238-
>>>>>>> c56e6a57ac3497aab77128c5bfca13513980709b
239221
coffeeMachine.#waterLimit = 1000; // Error
240222
*/!*
241-
242-
coffeeMachine.waterAmount = 100; // Works
243223
```
244224

245225
On the language level, `#` is a special sign that the field is private. We can't access it from outside or from inheriting classes.
@@ -285,17 +265,17 @@ class MegaCoffeeMachine extends CoffeeMachine {
285265

286266
In many scenarios such limitation is too severe. If we extend a `CoffeeMachine`, we may have legitimate reasons to access its internals. That's why protected fields are used more often, even though they are not supported by the language syntax.
287267

288-
````warn
268+
````warn header="Private fields are not available as this[name]"
289269
Private fields are special.
290270

291-
Remember, usually we can access fields by this[name]:
271+
As we know, usually we can access fields using `this[name]`:
292272

293273
```js
294274
class User {
295275
...
296276
sayHi() {
297277
let fieldName = "name";
298-
alert(`Hello, ${this[fieldName]}`);
278+
alert(`Hello, ${*!*this[fieldName]*/!*}`);
299279
}
300280
}
301281
```
@@ -321,11 +301,11 @@ Protection for users, so that they don't shoot themselves in the foot
321301
Supportable
322302
: The situation in programming is more complex than with a real-life coffee machine, because we don't just buy it once. The code constantly undergoes development and improvement.
323303

324-
**If we strictly delimit the internal interface, then the developer of the class can freely change its internal properties and methods, even without informing the users..**
304+
**If we strictly delimit the internal interface, then the developer of the class can freely change its internal properties and methods, even without informing the users.**
325305

326-
It's much easier to develop, if you know that certain methods can be renamed, their parameters can be changed, and even removed, because no external code depends on them.
306+
If you're a developer of such class, it's great to know that private methods can be safely renamed, their parameters can be changed, and even removed, because no external code depends on them.
327307

328-
For users, when a new version comes out, it may be a total overhaul, but still simple to upgrade if the external interface is the same.
308+
For users, when a new version comes out, it may be a total overhaul internally, but still simple to upgrade if the external interface is the same.
329309

330310
Hiding complexity
331311
: People adore using things that are simple. At least from outside. What's inside is a different thing.

0 commit comments

Comments
 (0)