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
The most intricate thing happens with strings, numbers and booleans.
101
101
102
-
As we remember, they are not objects. But if we try to access their properties, then temporary wrapper objects are created using built-in constructors `String`, `Number`, `Boolean`, they provide the methods and disappear.
102
+
As we remember, they are not objects. But if we try to access their properties, temporary wrapper objects are created using built-in constructors `String`, `Number` and `Boolean`. They provide the methods and disappear.
103
103
104
104
These objects are created invisibly to us and most engines optimize them out, but the specification describes it exactly this way. Methods of these objects also reside in prototypes, available as `String.prototype`, `Number.prototype` and `Boolean.prototype`.
105
105
106
106
```warn header="Values `null` and `undefined` have no object wrappers"
107
-
Special values `null` and `undefined` stand apart. They have no object wrappers, so methods and properties are not available for them. And there are no corresponding prototypes too.
107
+
Special values `null` and `undefined` stand apart. They have no object wrappers, so methods and properties are not available for them. And there are no corresponding prototypes either.
@@ -129,9 +129,9 @@ So, generally, modifying a native prototype is considered a bad idea.
129
129
130
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 is not yet supported by current JavaScript engine.
132
+
Polyfilling is a term for making a substitute for a method that exists in the JavaScript specification, but is not yet supported by a particular JavaScript engine.
133
133
134
-
Then we may implement it manually and populate the built-in prototype with it.
134
+
We may then implement it manually and populate the built-in prototype with it.
135
135
136
136
For instance:
137
137
@@ -144,7 +144,7 @@ if (!String.prototype.repeat) { // if there's no such method
144
144
145
145
// actually, the code should be a little bit more complex than that
146
146
// (the full algorithm is in the specification)
147
-
// but even an imperfect polyfill is often considered good enough for use
147
+
// but even an imperfect polyfill is often considered good enough
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.
182
+
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 if the object is indeed an array. Many built-in methods are like that.
183
183
184
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
188
-
Borrowing methods is flexible, it allows to mix functionality from different objects if needed.
188
+
Borrowing methods is flexible, it allows to mix functionalities from different objects if needed.
189
189
190
190
## Summary
191
191
192
192
- All built-in objects follow the same pattern:
193
-
- The methods are stored in the prototype (`Array.prototype`, `Object.prototype`, `Date.prototype` etc).
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`. Only `undefined` and `null` do not have wrapper objects.
196
-
- Built-in prototypes can be modified or populated with new methods. But it's not recommended to change them. Probably the only allowable case is when we add-in a new standard, but not yet supported by the engine JavaScript method.
193
+
- The methods are stored in the prototype (`Array.prototype`, `Object.prototype`, `Date.prototype`, etc.)
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` and `Boolean.prototype`. Only `undefined` and `null` do not have wrapper objects
196
+
- Built-in prototypes can be modified or populated with new methods. But it's not recommended to change them. The only allowable case is probably when we add-in a new standard, but it's not yet supported by the JavaScript engine
0 commit comments