This commit is contained in:
Ilya Kantor 2021-05-16 12:01:01 +03:00
parent 4177be3612
commit 80956ed877
2 changed files with 10 additions and 5 deletions

View file

@ -61,7 +61,7 @@ If you're curious to see a concrete example of such an error, check this code ou
No need to think about the meaning of the brackets `[]` and `forEach` yet. We'll study them later. For now, just remember the result of the code: it shows `1` then `2`. No need to think about the meaning of the brackets `[]` and `forEach` yet. We'll study them later. For now, just remember the result of the code: it shows `1` then `2`.
Now, let's add an `alert` before the code and *not* finish it with a semicolon: Let's add an `alert` before the code and *not* finish it with a semicolon:
```js run no-beautify ```js run no-beautify
alert("There will be an error") alert("There will be an error")
@ -69,7 +69,7 @@ alert("There will be an error")
[1, 2].forEach(alert) [1, 2].forEach(alert)
``` ```
Now if we run the code, only the first `alert` is shown and then we have an error! Now if we run the code, only the first `alert` shows, and then we have an error!
But everything is fine again if we add a semicolon after `alert`: But everything is fine again if we add a semicolon after `alert`:
```js run ```js run

View file

@ -38,7 +38,12 @@ Why `user2.name` is `undefined`?
Here's how `new user.constructor('Pete')` works: Here's how `new user.constructor('Pete')` works:
1. First, it looks for `constructor` in `user`. Nothing. 1. First, it looks for `constructor` in `user`. Nothing.
2. Then it follows the prototype chain. The prototype of `user` is `User.prototype`, and it also has nothing. 2. Then it follows the prototype chain. The prototype of `user` is `User.prototype`, and it also has no `constructor` (because we "forgot" to set it right!).
3. The value of `User.prototype` is a plain object `{}`, its prototype is `Object.prototype`. And there is `Object.prototype.constructor == Object`. So it is used. 3. Going further up the chain, `User.prototype` is a plain object, its prototype is the built-in `Object.prototype`.
4. Finally, for the built-in `Object.prototype`, there's a built-in `Object.prototype.constructor == Object`. So it is used.
At the end, we have `let user2 = new Object('Pete')`. The built-in `Object` constructor ignores arguments, it always creates an empty object, similar to `let user2 = {}`, that's what we have in `user2` after all. Finally, at the end, we have `let user2 = new Object('Pete')`.
Probably, that's not what we want. We'd like to create `new User`, not `new Object`. That's the outcome of the missing `constructor`.
(Just in case you're curious, the `new Object(...)` call converts its argument to an object. That's a theoretical thing, in practice no one calls `new Object` with a value, and generally we don't use `new Object` to make objects at all).