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/02-first-steps/02-structure/article.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -61,15 +61,15 @@ If you're curious to see a concrete example of such an error, check this code ou
61
61
62
62
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`.
63
63
64
-
Now, let's add an `alert` before the code and *not* finish it with a semicolon:
64
+
Let's add an `alert` before the code and *not* finish it with a semicolon:
65
65
66
66
```js run no-beautify
67
67
alert("There will be an error")
68
68
69
69
[1, 2].forEach(alert)
70
70
```
71
71
72
-
Now if we run the code, only the first `alert` is shown and then we have an error!
72
+
Now if we run the code, only the first `alert` shows, and then we have an error!
73
73
74
74
But everything is fine again if we add a semicolon after `alert`:
Copy file name to clipboardExpand all lines: 1-js/08-prototypes/02-function-prototype/4-new-object-same-constructor/solution.md
+8-3Lines changed: 8 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,7 +38,12 @@ Why `user2.name` is `undefined`?
38
38
Here's how `new user.constructor('Pete')` works:
39
39
40
40
1. First, it looks for `constructor` in `user`. Nothing.
41
-
2. Then it follows the prototype chain. The prototype of `user` is `User.prototype`, and it also has nothing.
42
-
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.
41
+
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!).
42
+
3. Going further up the chain, `User.prototype` is a plain object, its prototype is the built-in `Object.prototype`.
43
+
4. Finally, for the built-in `Object.prototype`, there's a built-in `Object.prototype.constructor == Object`. So it is used.
43
44
44
-
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.
45
+
Finally, at the end, we have `let user2 = new Object('Pete')`.
46
+
47
+
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`.
48
+
49
+
(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).
0 commit comments