Skip to content

Commit 2def53f

Browse files
authored
Merge pull request #186 from javascript-tutorial/sync-0599d07b
Sync with upstream @ 0599d07
2 parents b757f4f + 6600079 commit 2def53f

File tree

17 files changed

+375
-675
lines changed

17 files changed

+375
-675
lines changed

1-js/02-first-steps/18-javascript-specials/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ Assignments
144144
: There is a simple assignment: `a = b` and combined ones like `a *= 2`.
145145

146146
Bitwise
147-
: Bitwise operators work with 32-bit integers at the lowest, bit-level: see the [docs](mdn:/JavaScript/Reference/Operators/Bitwise_Operators) when they are needed.
147+
: Bitwise operators work with 32-bit integers at the lowest, bit-level: see the [docs](mdn:/JavaScript/Guide/Expressions_and_Operators#Bitwise) when they are needed.
148148

149149
Conditional
150150
: The only operator with three parameters: `cond ? resultA : resultB`. If `cond` is truthy, returns `resultA`, otherwise `resultB`.

1-js/04-object-basics/01-object/8-multiply-numeric/task.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
importância: 3
1+
importance: 3
22

33
---
44

1-js/04-object-basics/02-object-copy/article.md

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
# Object copying, references
1+
# Object references and copying
22

3-
One of the fundamental differences of objects vs primitives is that they are stored and copied "by reference".
3+
One of the fundamental differences of objects versus primitives is that objects are stored and copied "by reference", as opposed to primitive values: strings, numbers, booleans, etc -- that are always copied "as a whole value".
44

5-
Primitive values: strings, numbers, booleans -- are assigned/copied "as a whole value".
5+
That's easy to understand if we look a bit "under a cover" of what happens when we copy a value.
66

7-
For instance:
7+
Let's start with a primitive, such as a string.
8+
9+
Here we put a copy of `message` into `phrase`:
810

911
```js
1012
let message = "Hello!";
@@ -15,21 +17,31 @@ As a result we have two independent variables, each one is storing the string `"
1517

1618
![](variable-copy-value.svg)
1719

20+
Quite an obvious result, right?
21+
1822
Objects are not like that.
1923

20-
**A variable stores not the object itself, but its "address in memory", in other words "a reference" to it.**
24+
**A variable assigned to an object stores not the object itself, but its "address in memory", in other words "a reference" to it.**
2125

22-
Here's the picture for the object:
26+
Let's look at an example of such variable:
2327

2428
```js
2529
let user = {
2630
name: "John"
2731
};
2832
```
2933

34+
And here's how it's actually stored in memory:
35+
3036
![](variable-contains-reference.svg)
3137

32-
Here, the object is stored somewhere in memory. And the variable `user` has a "reference" to it.
38+
The object is stored somewhere in memory (at the right of the picture), while the `user` variable (at the left) has a "reference" to it.
39+
40+
We may think of an object variable, such as `user`, as of a sheet of paper with the address.
41+
42+
When we perform actions with the object, e.g. take a property `user.name`, JavaScript engine looks into that address and performs the operation on the actual object.
43+
44+
Now here's why it's important.
3345

3446
**When an object variable is copied -- the reference is copied, the object is not duplicated.**
3547

@@ -45,6 +57,8 @@ Now we have two variables, each one with the reference to the same object:
4557

4658
![](variable-copy-reference.svg)
4759

60+
As you can see, there's still one object, now with two variables that reference it.
61+
4862
We can use any variable to access the object and modify its contents:
4963

5064
```js run
@@ -59,15 +73,14 @@ admin.name = 'Pete'; // changed by the "admin" reference
5973
alert(*!*user.name*/!*); // 'Pete', changes are seen from the "user" reference
6074
```
6175
62-
The example above demonstrates that there is only one object. As if we had a cabinet with two keys and used one of them (`admin`) to get into it. Then, if we later use another key (`user`) we can see changes.
6376
64-
## Comparison by reference
77+
It's just as if we had a cabinet with two keys and used one of them (`admin`) to get into it. Then, if we later use another key (`user`) we can see changes.
6578
66-
The equality `==` and strict equality `===` operators for objects work exactly the same.
79+
## Comparison by reference
6780
68-
**Two objects are equal only if they are the same object.**
81+
Two objects are equal only if they are the same object.
6982
70-
Here two variables reference the same object, thus they are equal:
83+
For instance, here `a` and `b` reference the same object, thus they are equal:
7184
7285
```js run
7386
let a = {};
@@ -77,7 +90,7 @@ alert( a == b ); // true, both variables reference the same object
7790
alert( a === b ); // true
7891
```
7992
80-
And here two independent objects are not equal, even though both are empty:
93+
And here two independent objects are not equal, even though they look alike (both are empty):
8194
8295
```js run
8396
let a = {};
@@ -86,7 +99,7 @@ let b = {}; // two independent objects
8699
alert( a == b ); // false
87100
```
88101
89-
For comparisons like `obj1 > obj2` or for a comparison against a primitive `obj == 5`, objects are converted to primitives. We'll study how object conversions work very soon, but to tell the truth, such comparisons occur very rarely, usually as a result of a coding mistake.
102+
For comparisons like `obj1 > obj2` or for a comparison against a primitive `obj == 5`, objects are converted to primitives. We'll study how object conversions work very soon, but to tell the truth, such comparisons are needed very rarely, usually they appear as a result of a programming mistake.
90103
91104
## Cloning and merging, Object.assign
92105
@@ -215,11 +228,6 @@ alert(clone.sizes.width); // 51, see the result from the other one
215228
216229
To fix that, we should use the cloning loop that examines each value of `user[key]` and, if it's an object, then replicate its structure as well. That is called a "deep cloning".
217230
218-
<<<<<<< HEAD
219-
There's a standard algorithm for deep cloning that handles the case above and more complex cases, called the [Structured cloning algorithm](https://html.spec.whatwg.org/multipage/structured-data.html#safe-passing-of-structured-data).
220-
221-
=======
222-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3
223231
We can use recursion to implement it. Or, not to reinvent the wheel, take an existing implementation, for instance [_.cloneDeep(obj)](https://lodash.com/docs#cloneDeep) from the JavaScript library [lodash](https://lodash.com).
224232
225233
## Summary

1-js/04-object-basics/04-object-methods/article.md

Lines changed: 19 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Actions are represented in JavaScript by functions in properties.
1515

1616
## Method examples
1717

18-
For the start, let's teach the `user` to say hello:
18+
For a start, let's teach the `user` to say hello:
1919

2020
```js run
2121
let user = {
@@ -32,11 +32,11 @@ user.sayHi = function() {
3232
user.sayHi(); // Hello!
3333
```
3434

35-
Here we've just used a Function Expression to create the function and assign it to the property `user.sayHi` of the object.
35+
Here we've just used a Function Expression to create a function and assign it to the property `user.sayHi` of the object.
3636

37-
Then we can call it. The user can now speak!
37+
Then we can call it as `user.sayHi()`. The user can now speak!
3838

39-
A function that is the property of an object is called its *method*.
39+
A function that is a property of an object is called its *method*.
4040

4141
So, here we've got a method `sayHi` of the object `user`.
4242

@@ -63,11 +63,7 @@ user.sayHi(); // Hello!
6363
```smart header="Object-oriented programming"
6464
When we write our code using objects to represent entities, that's called [object-oriented programming](https://en.wikipedia.org/wiki/Object-oriented_programming), in short: "OOP".
6565
66-
<<<<<<< HEAD
67-
OOP is a big thing, an interesting science of its own. How to choose the right entities? How to organize the interaction between them? That's architecture, and there are great books on that topic, like "Design Patterns: Elements of Reusable Object-Oriented Software" by E.Gamma, R.Helm, R.Johnson, J.Vissides or "Object-Oriented Analysis and Design with Applications" by G.Booch, and more.
68-
=======
6966
OOP is a big thing, an interesting science of its own. How to choose the right entities? How to organize the interaction between them? That's architecture, and there are great books on that topic, like "Design Patterns: Elements of Reusable Object-Oriented Software" by E. Gamma, R. Helm, R. Johnson, J. Vissides or "Object-Oriented Analysis and Design with Applications" by G. Booch, and more.
70-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3
7167
```
7268
### Method shorthand
7369

@@ -76,14 +72,14 @@ There exists a shorter syntax for methods in an object literal:
7672
```js
7773
// these objects do the same
7874

79-
let user = {
75+
user = {
8076
sayHi: function() {
8177
alert("Hello");
8278
}
8379
};
8480

8581
// method shorthand looks better, right?
86-
let user = {
82+
user = {
8783
*!*
8884
sayHi() { // same as "sayHi: function()"
8985
*/!*
@@ -115,6 +111,7 @@ let user = {
115111

116112
sayHi() {
117113
*!*
114+
// "this" is the "current object"
118115
alert(this.name);
119116
*/!*
120117
}
@@ -163,18 +160,16 @@ let user = {
163160
let admin = user;
164161
user = null; // overwrite to make things obvious
165162

166-
admin.sayHi(); // Whoops! inside sayHi(), the old name is used! error!
163+
*!*
164+
admin.sayHi(); // TypeError: Cannot read property 'name' of null
165+
*/!*
167166
```
168167

169168
If we used `this.name` instead of `user.name` inside the `alert`, then the code would work.
170169

171170
## "this" is not bound
172171

173-
<<<<<<< HEAD
174-
In JavaScript, "this" keyword behaves unlike most other programming languages. First, it can be used in any function.
175-
=======
176-
In JavaScript, keyword `this` behaves unlike most other programming languages. It can be used in any function.
177-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3
172+
In JavaScript, keyword `this` behaves unlike most other programming languages. It can be used in any function, even if it's not a method of an object.
178173

179174
There's no syntax error in the following example:
180175

@@ -184,9 +179,9 @@ function sayHi() {
184179
}
185180
```
186181

187-
The value of `this` is evaluated during the run-time. And it can be anything.
182+
The value of `this` is evaluated during the run-time, depending on the context.
188183

189-
For instance, the same function may have different "this" when called from different objects:
184+
For instance, here the same function is assigned to two different objects and has different "this" in the calls:
190185

191186
```js run
192187
let user = { name: "John" };
@@ -197,7 +192,7 @@ function sayHi() {
197192
}
198193

199194
*!*
200-
// use the same functions in two objects
195+
// use the same function in two objects
201196
user.f = sayHi;
202197
admin.f = sayHi;
203198
*/!*
@@ -210,7 +205,10 @@ admin.f(); // Admin (this == admin)
210205
admin['f'](); // Admin (dot or square brackets access the method – doesn't matter)
211206
```
212207

213-
Actually, we can call the function without an object at all:
208+
The rule is simple: if `obj.f()` is called, then `this` is `obj` during the call of `f`. So it's either `user` or `admin` in the example above.
209+
210+
````smart header="Calling without an object: `this == undefined`"
211+
We can even call the function without an object at all:
214212

215213
```js run
216214
function sayHi() {
@@ -224,12 +222,8 @@ In this case `this` is `undefined` in strict mode. If we try to access `this.nam
224222

225223
In non-strict mode the value of `this` in such case will be the *global object* (`window` in a browser, we'll get to it later in the chapter [](info:global-object)). This is a historical behavior that `"use strict"` fixes.
226224

227-
<<<<<<< HEAD
228-
Please note that usually a call of a function that uses `this` without an object is not normal, but rather a programming mistake. If a function has `this`, then it is usually meant to be called in the context of an object.
229-
=======
230225
Usually such call is a programming error. If there's `this` inside a function, it expects to be called in an object context.
231226
````
232-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3
233227
234228
```smart header="The consequences of unbound `this`"
235229
If you come from another programming language, then you are probably used to the idea of a "bound `this`", where methods defined in an object always have `this` referencing that object.
@@ -241,99 +235,6 @@ The concept of run-time evaluated `this` has both pluses and minuses. On the one
241235
Here our position is not to judge whether this language design decision is good or bad. We'll understand how to work with it, how to get benefits and avoid problems.
242236
```
243237
244-
<<<<<<< HEAD
245-
## Internals: Reference Type
246-
247-
```warn header="In-depth language feature"
248-
This section covers an advanced topic, to understand certain edge-cases better.
249-
250-
If you want to go on faster, it can be skipped or postponed.
251-
```
252-
253-
An intricate method call can lose `this`, for instance:
254-
255-
```js run
256-
let user = {
257-
name: "John",
258-
hi() { alert(this.name); },
259-
bye() { alert("Bye"); }
260-
};
261-
262-
user.hi(); // John (the simple call works)
263-
264-
*!*
265-
// now let's call user.hi or user.bye depending on the name
266-
(user.name == "John" ? user.hi : user.bye)(); // Error!
267-
*/!*
268-
```
269-
270-
On the last line there is a ternary operator that chooses either `user.hi` or `user.bye`. In this case the result is `user.hi`.
271-
272-
The method is immediately called with parentheses `()`. But it doesn't work right!
273-
274-
You can see that the call results in an error, because the value of `"this"` inside the call becomes `undefined`.
275-
276-
This works (object dot method):
277-
```js
278-
user.hi();
279-
```
280-
281-
This doesn't (evaluated method):
282-
```js
283-
(user.name == "John" ? user.hi : user.bye)(); // Error!
284-
```
285-
286-
Why? If we want to understand why it happens, let's get under the hood of how `obj.method()` call works.
287-
288-
Looking closely, we may notice two operations in `obj.method()` statement:
289-
290-
1. First, the dot `'.'` retrieves the property `obj.method`.
291-
2. Then parentheses `()` execute it.
292-
293-
So, how does the information about `this` get passed from the first part to the second one?
294-
295-
If we put these operations on separate lines, then `this` will be lost for sure:
296-
297-
```js run
298-
let user = {
299-
name: "John",
300-
hi() { alert(this.name); }
301-
}
302-
303-
*!*
304-
// split getting and calling the method in two lines
305-
let hi = user.hi;
306-
hi(); // Error, because this is undefined
307-
*/!*
308-
```
309-
310-
Here `hi = user.hi` puts the function into the variable, and then on the last line it is completely standalone, and so there's no `this`.
311-
312-
**To make `user.hi()` calls work, JavaScript uses a trick -- the dot `'.'` returns not a function, but a value of the special [Reference Type](https://tc39.github.io/ecma262/#sec-reference-specification-type).**
313-
314-
The Reference Type is a "specification type". We can't explicitly use it, but it is used internally by the language.
315-
316-
The value of Reference Type is a three-value combination `(base, name, strict)`, where:
317-
318-
- `base` is the object.
319-
- `name` is the property.
320-
- `strict` is true if `use strict` is in effect.
321-
322-
The result of a property access `user.hi` is not a function, but a value of Reference Type. For `user.hi` in strict mode it is:
323-
324-
```js
325-
// Reference Type value
326-
(user, "hi", true)
327-
```
328-
329-
When parentheses `()` are called on the Reference Type, they receive the full information about the object and its method, and can set the right `this` (`=user` in this case).
330-
331-
Any other operation like assignment `hi = user.hi` discards the reference type as a whole, takes the value of `user.hi` (a function) and passes it on. So any further operation "loses" `this`.
332-
333-
So, as the result, the value of `this` is only passed the right way if the function is called directly using a dot `obj.method()` or square brackets `obj['method']()` syntax (they do the same here). Later in this tutorial, we will learn various ways to solve this problem such as [func.bind()](/bind#solution-2-bind).
334-
335-
=======
336-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3
337238
## Arrow functions have no "this"
338239
339240
Arrow functions are special: they don't have their "own" `this`. If we reference `this` from such a function, it's taken from the outer "normal" function.
@@ -363,7 +264,7 @@ That's a special feature of arrow functions, it's useful when we actually do not
363264
364265
The value of `this` is defined at run-time.
365266
- When a function is declared, it may use `this`, but that `this` has no value until the function is called.
366-
- That function can be copied between objects.
267+
- A function can be copied between objects.
367268
- When a function is called in the "method" syntax: `object.method()`, the value of `this` during the call is `object`.
368269
369270
Please note that arrow functions are special: they have no `this`. When `this` is accessed inside an arrow function, it is taken from outside.

0 commit comments

Comments
 (0)