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
Where's the code that generates the string `"[object Object]"`? That's a built-in `toString` method, but where is it? The `obj` is empty!
17
17
18
-
...But the short notation `obj = {}` is the same as `obj = new Object()`, where `Object`-- is a built-in object constructor function. And that function has `Object.prototype`that references a huge object with `toString` and other functions.
18
+
...But the short notation `obj = {}` is the same as `obj = new Object()`, where `Object` is a built-in object constructor function, with its own `prototype`referencing a huge object with `toString` and other methods.
19
19
20
-
Like this (all that is built-in):
20
+
Here's what's going on:
21
21
22
22

23
23
24
-
When `new Object()` is called (or a literal object `{...}` is created), the `[[Prototype]]` of it is set to `Object.prototype`by the rule that we've discussed in the previous chapter:
24
+
When `new Object()` is called (or a literal object `{...}` is created), the `[[Prototype]]` of it is set to `Object.prototype`according to the rule that we discussed in the previous chapter:
25
25
26
26

27
27
28
-
Afterwards when `obj.toString()` is called -- the method is taken from `Object.prototype`.
28
+
So then when `obj.toString()` is called the method is taken from `Object.prototype`.
29
29
30
30
We can check it like this:
31
31
@@ -48,7 +48,7 @@ Other built-in objects such as `Array`, `Date`, `Function` and others also keep
48
48
49
49
For instance, when we create an array `[1, 2, 3]`, the default `new Array()` constructor is used internally. So the array data is written into the new object, and `Array.prototype` becomes its prototype and provides methods. That's very memory-efficient.
50
50
51
-
By specification, all built-in prototypes have `Object.prototype` on the top. Sometimes people say that "everything inherits from objects".
51
+
By specification, all of the built-in prototypes have `Object.prototype` on the top. Sometimes people say that "everything inherits from objects".
52
52
53
53
Here's the overall picture (for 3 built-ins to fit):
54
54
@@ -82,11 +82,11 @@ As we've seen before, `Object.prototype` has `toString` as well, but `Array.prot
82
82

83
83
84
84
85
-
In-browser tools like Chrome developer console also show inheritance (may need to use `console.dir` for built-in objects):
85
+
In-browser tools like Chrome developer console also show inheritance (`console.dir`may need to be used for built-in objects):
86
86
87
87

88
88
89
-
Other built-in objects also work the same way. Even functions. They are objects of a built-in `Function` constructor, and their methods: `call/apply` and others are taken from `Function.prototype`. Functions have their own `toString` too.
89
+
Other built-in objects also work the same way. Even functions -- they are objects of a built-in `Function` constructor, and their methods (`call`/`apply` and others) are taken from `Function.prototype`. Functions have their own `toString` too.
During the process of development we may have ideas which new built-in methods we'd like to have. And there may be a slight temptation to add them to native prototypes. But that is generally a bad idea.
122
+
During the process of development, we may have ideas for new built-in methods we'd like to have, and we may be tempted to add them to native prototypes. But that is generally a bad idea.
123
123
124
124
```warn
125
-
Prototypes are global, so it's easy to get a conflict. If two libraries add a method `String.prototype.show`, then one of them overwrites the other one.
125
+
Prototypes are global, so it's easy to get a conflict. If two libraries add a method `String.prototype.show`, then one of them will be overwriting the other.
126
126
127
-
So, generally modifying a native prototypeis considered a bad idea.
127
+
So, generally, modifying a native prototype is considered a bad idea.
128
128
```
129
129
130
-
**In modern programming, there is only one case when modifying native prototypes is approved. That's polyfilling.**
130
+
**In modern programming, there is only one case where modifying native prototypes is approved. That's polyfilling.**
131
131
132
-
Polyfilling is a term for making a substitute for a method that exists in JavaScript specification, but not yet supported by current JavaScript engine.
132
+
Polyfilling is a term for making a substitute for a method that exists in JavaScript specification, but not yet supported by current JavaScript engine.
133
133
134
134
Then we may implement it manually and populate the built-in prototype with it.
It works, because the internal algorithm of the built-in `join` method only cares about the correct indexes and the `length` property, it doesn't check that the object is indeed the array. And many built-in methods are like that.
183
183
184
-
Another possibility is to inherit by setting `obj.__proto__` to `Array.prototype`, then all `Array` methods are automatically available in `obj`.
184
+
Another possibility is to inherit by setting `obj.__proto__` to `Array.prototype`, so all `Array` methods are automatically available in `obj`.
185
185
186
186
But that's impossible if `obj` already inherits from another object. Remember, we only can inherit from one object at a time.
187
187
@@ -192,5 +192,5 @@ Borrowing methods is flexible, it allows to mix functionality from different obj
192
192
- All built-in objects follow the same pattern:
193
193
- The methods are stored in the prototype (`Array.prototype`, `Object.prototype`, `Date.prototype` etc).
194
194
- The object itself stores only the data (array items, object properties, the date).
195
-
- Primitives also store methods in prototypes of wrapper objects: `Number.prototype`, `String.prototype`, `Boolean.prototype`. There are no wrapper objects only for `undefined` and `null`.
195
+
- Primitives also store methods in prototypes of wrapper objects: `Number.prototype`, `String.prototype`, `Boolean.prototype`. Only `undefined` and `null` do not have wrapper objects.
196
196
- Built-in prototypes can be modified or populated with new methods. But it's not recommended to change them. Probably the only allowable cause is when we add-in a new standard, but not yet supported by the engine JavaScript method.
0 commit comments