Skip to content

Commit 8642ab9

Browse files
authored
Merge branch 'master' into conflicts-check-after-pr178
2 parents 672c343 + 63cbf4a commit 8642ab9

File tree

20 files changed

+465
-409
lines changed

20 files changed

+465
-409
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 & 14 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

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

Lines changed: 19 additions & 13 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

@@ -72,14 +72,14 @@ There exists a shorter syntax for methods in an object literal:
7272
```js
7373
// these objects do the same
7474

75-
let user = {
75+
user = {
7676
sayHi: function() {
7777
alert("Hello");
7878
}
7979
};
8080

8181
// method shorthand looks better, right?
82-
let user = {
82+
user = {
8383
*!*
8484
sayHi() { // same as "sayHi: function()"
8585
*/!*
@@ -111,6 +111,7 @@ let user = {
111111

112112
sayHi() {
113113
*!*
114+
// "this" is the "current object"
114115
alert(this.name);
115116
*/!*
116117
}
@@ -159,14 +160,16 @@ let user = {
159160
let admin = user;
160161
user = null; // overwrite to make things obvious
161162

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

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

167170
## "this" is not bound
168171

169-
In JavaScript, keyword `this` behaves unlike most other programming languages. It can be used in any function.
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.
170173

171174
There's no syntax error in the following example:
172175

@@ -176,9 +179,9 @@ function sayHi() {
176179
}
177180
```
178181

179-
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.
180183

181-
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:
182185

183186
```js run
184187
let user = { name: "John" };
@@ -189,7 +192,7 @@ function sayHi() {
189192
}
190193

191194
*!*
192-
// use the same functions in two objects
195+
// use the same function in two objects
193196
user.f = sayHi;
194197
admin.f = sayHi;
195198
*/!*
@@ -202,7 +205,10 @@ admin.f(); // Admin (this == admin)
202205
admin['f'](); // Admin (dot or square brackets access the method – doesn't matter)
203206
```
204207

205-
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:
206212

207213
```js run
208214
function sayHi() {
@@ -258,7 +264,7 @@ That's a special feature of arrow functions, it's useful when we actually do not
258264
259265
The value of `this` is defined at run-time.
260266
- When a function is declared, it may use `this`, but that `this` has no value until the function is called.
261-
- That function can be copied between objects.
267+
- A function can be copied between objects.
262268
- When a function is called in the "method" syntax: `object.method()`, the value of `this` during the call is `object`.
263269
264270
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)