Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 80956ed

Browse files
committed
closes #2600
1 parent 4177be3 commit 80956ed

File tree

2 files changed

+10
-5
lines changed
  • 1-js
    • 02-first-steps/02-structure
    • 08-prototypes/02-function-prototype/4-new-object-same-constructor

2 files changed

+10
-5
lines changed

‎1-js/02-first-steps/02-structure/article.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ If you're curious to see a concrete example of such an error, check this code ou
6161
6262
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`.
6363
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:
6565
6666
```js run no-beautify
6767
alert("There will be an error")
6868
6969
[1, 2].forEach(alert)
7070
```
7171
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!
7373
7474
But everything is fine again if we add a semicolon after `alert`:
7575
```js run

‎1-js/08-prototypes/02-function-prototype/4-new-object-same-constructor/solution.md‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ Why `user2.name` is `undefined`?
3838
Here's how `new user.constructor('Pete')` works:
3939

4040
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.
4344

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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /