You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/4-object-basics/04-object-methods/article.md
+23-1Lines changed: 23 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -219,10 +219,11 @@ Please note that usually a call of a function using `this` without an object is
219
219
```smart header="The consequences of unbound `this`"
220
220
If you come from another programming languages, then you are probably used to an idea of a "bound `this`", where methods defined in an object always have `this` referencing that object.
221
221
222
-
The idea of unbound, run-time evaluated `this` has both pluses and minuses. From one side, a function can be reused for different objects. From the other side, greater flexibility opens a place for mistakes.
222
+
The idea of unbound, run-time evaluated `this` has both pluses and minuses. From one side, a function can be reused for different objects. From the other side, greater flexibility opens a place for mistakes.
223
223
224
224
Here we are not to judge whether this language design decision is good or bad. We will understand how to work with it, how to get benefits and evade problems.
225
225
```
226
+
226
227
## Internals: Reference Type
227
228
228
229
An intricate method call can loose `this`, for instance:
@@ -295,6 +296,27 @@ Any other operation like assignment `hi = user.hi` discards the reference type a
295
296
296
297
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).
297
298
299
+
````warn header="Arrow functions do not have `this`"
300
+
Arrow functions are special: they don't have "own" `this`. If we reference `this` from such function, it's taken from the outer "normal" function.
301
+
302
+
For instance, here `arrow()` uses `this` from the outer `user.sayHi()` method:
303
+
304
+
```js run
305
+
let user = {
306
+
firstName:"Ilya",
307
+
sayHi() {
308
+
letarrow= () =>alert(this.firstName);
309
+
arrow();
310
+
}
311
+
};
312
+
313
+
user.sayHi(); // Ilya
314
+
```
315
+
316
+
That's a special feature of arrow functions, it's useful when we actually do not want to have a separate `this`, but rather to take it from the outer context. Later in the chapter <info:arrow-functions> we'll dig more deeply into what's going on.
Copy file name to clipboardExpand all lines: 1-js/5-data-types/01-primitives-methods/article.md
+33Lines changed: 33 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -81,6 +81,39 @@ alert( n.toFixed(2) ); // 1.23
81
81
82
82
We'll see more specific methods in chapters <info:number> and <info:string>.
83
83
84
+
85
+
````warn header="Constructors `String/Number/Boolean` are for internal use only"
86
+
Some languages like Java allow to create "wrapper objects" for primitives explicitly using syntax like `new Number(1)` or `new Boolean(false)`.
87
+
88
+
In Javascript that's also possible for historical reasons, but highly not recommended. Things will go crazy in many places.
89
+
90
+
For instance:
91
+
92
+
```js run
93
+
alert( typeof1 ); // "number"
94
+
95
+
alert( typeofnewNumber(1) ); // "object"!
96
+
```
97
+
98
+
And, because `zero` is an object:
99
+
100
+
```js run
101
+
let zero =newNumber(0);
102
+
103
+
if (zero) { // zero is true, because it's an object
104
+
alert( "zero is truthy?!?" );
105
+
}
106
+
```
107
+
108
+
From the other side, using same functions `String/Number/Boolean` without `new` is a totally sane and useful thing. They convert a value to the corresponding type: to a string, a number, or a boolean (primitive).
109
+
110
+
This is totally valid:
111
+
```js
112
+
let num =Number("123"); // convert a string to number
113
+
```
114
+
````
115
+
116
+
84
117
````warn header="null/undefined have no methods"
85
118
Special primitives `null` and `undefined` are exceptions. They have no corresponding "wrapper objects" and provide no methods. In a sense, they are "the most primitive".
Copy file name to clipboardExpand all lines: 1-js/5-data-types/05-array-methods/article.md
+12-13Lines changed: 12 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -606,32 +606,31 @@ alert(typeof {}); // object
606
606
alert(typeof []); // same
607
607
```
608
608
609
-
There's a special method for that [Array.isArray(value)](mdn:js/Array/isArray) that returns `true` if the `value` is an array, and `false` otherwise.
609
+
...But arrays are used so often that there's a special method for that:[Array.isArray(value)](mdn:js/Array/isArray). It returns `true` if the `value` is an array, and `false` otherwise.
610
610
611
611
```js run
612
612
alert(Array.isArray({})); // false
613
613
614
614
alert(Array.isArray([])); // true
615
615
```
616
616
617
-
```smart header="`Array.isArray` vs other type-checks"
618
-
You remembeare other ways to check for
619
-
620
617
## Methods: "thisArg"
621
618
622
619
Almost all array methods that call functions -- like `find`, `filter`, `map`, with a notable exception of `sort`, accept an optional additional parameter `thisArg`.
623
620
624
-
The full syntax is:
621
+
In the sections above that parameter is not explained, because it's rarely used.
622
+
623
+
But for completeness here's the full syntax:
625
624
626
625
```js
627
-
let result =arr.find(func, thisArg);
628
-
let results =arr.filter(func, thisArg);
629
-
// etc, thisArg goes after the function
626
+
arr.find(func, thisArg);
627
+
arr.filter(func, thisArg);
628
+
arr.map(func, thisArg);
629
+
// ...
630
+
// thisArg is the optional last argument
630
631
```
631
632
632
-
It is used sparingly, but we have to cover it here for the sake of completeness.
633
-
634
-
The value of `thisArg` parameter becomes `this` for the function.
633
+
The value of `thisArg` parameter becomes `this` for `func`.
635
634
636
635
For instance, here we use an object method as a filter:
637
636
@@ -657,7 +656,7 @@ let youngerUsers = users.filter(user.younger, user);
657
656
alert(youngerUsers.length); // 2
658
657
```
659
658
660
-
In the call above, we use `user.younger` as a filter and also provide `user` as the context for it. If we did't provide the context, `users.filter(user.younger)` would call `user.younger` as a standalone function, with `this=undefined`. That would be an instant error.
659
+
In the call above, we use `user.younger` as a filter and also provide `user` as the context for it. If we did't provide the context, `users.filter(user.younger)` would call `user.younger` as a standalone function, with `this=undefined`. That would mean an instant error.
661
660
662
661
## Other methods
663
662
@@ -676,7 +675,7 @@ These and other methods are also listed in the [manual](mdn:js/Array).
676
675
677
676
## Summary
678
677
679
-
Most often methods:
678
+
Most used array methods:
680
679
681
680
-`split/join` -- convert a string to array and back.
682
681
-`splice` -- delete and insert elements at the given position.
Copy file name to clipboardExpand all lines: 1-js/8-more-functions/08-call-apply-decorators/article.md
-1Lines changed: 0 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -434,7 +434,6 @@ Taken from the specification almost "as-is":
434
434
435
435
So, technically it takes `this` and joins `this[0]`, `this[1]` ...etc together. It's intentionally written in a way that allows any array-like `this` (not a coincidence, many methods follow this practice). That's why it also works with `this=arguments`.
436
436
437
-
438
437
## Summary
439
438
440
439
*Decorator* is a wrapper around a function that alters its behavior. The main job is still carried out by the function.
0 commit comments