Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 83b93e5

Browse files
committedNov 22, 2016
up
1 parent 63f55dc commit 83b93e5

File tree

49 files changed

+689
-271
lines changed

Some content is hidden

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

49 files changed

+689
-271
lines changed
 

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,11 @@ Please note that usually a call of a function using `this` without an object is
219219
```smart header="The consequences of unbound `this`"
220220
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.
221221

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.
223223

224224
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.
225225
```
226+
226227
## Internals: Reference Type
227228
228229
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
295296

296297
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).
297298

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+
let arrow = () => 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.
317+
318+
````
319+
298320
## Summary
299321
300322
[todo]

‎1-js/5-data-types/01-primitives-methods/article.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,39 @@ alert( n.toFixed(2) ); // 1.23
8181

8282
We'll see more specific methods in chapters <info:number> and <info:string>.
8383

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( typeof 1 ); // "number"
94+
95+
alert( typeof new Number(1) ); // "object"!
96+
```
97+
98+
And, because `zero` is an object:
99+
100+
```js run
101+
let zero = new Number(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+
84117
````warn header="null/undefined have no methods"
85118
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".
86119

0 commit comments

Comments
 (0)
Please sign in to comment.