Skip to content

Commit c113522

Browse files
authored
smoothed over wording
improved grammar, removed extra punctuation (the "-"), generally attempted to make things make a little more sense, fixed a couple of typos
1 parent 0a95d04 commit c113522

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

1-js/08-prototypes/03-native-prototypes/article.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ alert( obj ); // "[object Object]" ?
1515

1616
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!
1717

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.
1919

20-
Like this (all that is built-in):
20+
Here's what's going on:
2121

2222
![](object-prototype.png)
2323

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:
2525

2626
![](object-prototype-1.png)
2727

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`.
2929

3030
We can check it like this:
3131

@@ -48,7 +48,7 @@ Other built-in objects such as `Array`, `Date`, `Function` and others also keep
4848

4949
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.
5050

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".
5252

5353
Here's the overall picture (for 3 built-ins to fit):
5454

@@ -82,11 +82,11 @@ As we've seen before, `Object.prototype` has `toString` as well, but `Array.prot
8282
![](native-prototypes-array-tostring.png)
8383

8484

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):
8686

8787
![](console_dir_array.png)
8888

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.
9090

9191
```js run
9292
function f() {}
@@ -119,17 +119,17 @@ String.prototype.show = function() {
119119
"BOOM!".show(); // BOOM!
120120
```
121121

122-
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.
123123

124124
```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.
126126
127-
So, generally modifying a native prototypeis considered a bad idea.
127+
So, generally, modifying a native prototype is considered a bad idea.
128128
```
129129

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.**
131131

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.
133133

134134
Then we may implement it manually and populate the built-in prototype with it.
135135

@@ -181,7 +181,7 @@ alert( obj.join(',') ); // Hello,world!
181181

182182
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.
183183

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`.
185185

186186
But that's impossible if `obj` already inherits from another object. Remember, we only can inherit from one object at a time.
187187

@@ -192,5 +192,5 @@ Borrowing methods is flexible, it allows to mix functionality from different obj
192192
- All built-in objects follow the same pattern:
193193
- The methods are stored in the prototype (`Array.prototype`, `Object.prototype`, `Date.prototype` etc).
194194
- 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.
196196
- 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

Comments
 (0)