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/2-first-steps/07-types/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
@@ -234,7 +234,7 @@ Here we have a variable `key` which contains the property name, probably evaluat
234
234
235
235
Most of time, the dot is used to access object properties, but when we need a complex property name or to pass the name as a variable, then -- we go square brackets.
236
236
237
-
Javascript supports object inheritance. There are many types that are based on objects:`Date`for dates, `Array`for ordered data, `Error`for error-reporting and so on. So the word "object" is applicable to a variety ofthings. The term *plain objects* is used to represent "basic" objects, the ones we create with`{ ... }`.
237
+
Javascript supports object inheritance. There are many types that are based on objects:`Date`for dates, `Array`for ordered data, `Error`for error-reporting and so on. So the word "object" is applicable to a variety ofthings. The term *plain objects*or just `Object` (capital first) is used to represent "basic" objects, the ones we create with`{ ... }`.
238
238
239
239
Objects in JavaScript are very powerful. Here we've just scratched the surface of the topic that is really huge. We'll be closely working with objects and learning more about them in further parts of the tutorial.
240
240
@@ -385,7 +385,7 @@ There are 7 basic types in JavaScript.
385
385
- `null` for unknown values.
386
386
- `undefined` for unassigned values.
387
387
- `symbol` for unique identifiers.
388
-
- `object` for more complex data structures (there exist many, we saw arrays).
388
+
- `object` for more complex data structures.
389
389
390
390
The `typeof` operator allows to see which type is stored in the variable.
Because `ToPrimitive` ignores `valueOf` if it returns an object, here we can assume that `valueOf` does not exist at all.
131
134
132
-
The result of these operations should be somewhat obvious now.
135
+
Now `toString`.
133
136
137
+
From the first sight it's obvious:
134
138
139
+
```js run
140
+
let obj = { };
135
141
142
+
alert( obj ); // [object Object]
143
+
```
136
144
145
+
But it's much more powerful than that.
137
146
147
+
By [specification](https://tc39.github.io/ecma262/#sec-object.prototype.tostring), `toString` can work in the context of any value. And it returns `[object ...]` with the type of an object instead of dots.
138
148
139
-
## [[Class]]
149
+
The algorithm of the `toString()` for plain objects looks like this:
140
150
141
-
From the chapter <info:types> we know that `typeof` cannot distinguish different kinds of objects. Arrays, plain objects and others are all the same "object" for it.
151
+
- If `this` value is `undefined`, return `[object Undefined]`
152
+
- If `this` value is `null`, return `[object Null]`
153
+
- ...For arrays return `[object Array]`, for dates return `[object Date]` etc.
142
154
143
-
But there's a semi-hidden way to access the right class.
144
155
145
-
Most built-in objects
156
+
It even works for environment-specific objects that exist only in the browser (like `window`) or in node.js (like `process`).
146
157
158
+
All we need to do to get the type of an `obj` -- is to call plain object `toString` passing `this = obj`.
147
159
160
+
We can do it like this:
148
161
162
+
```js run
163
+
let s = {}.toString; // copy toString of Object to a variable
149
164
150
-
Here are some built-in objects
165
+
// what type is this?
166
+
let arr = [];
151
167
152
-
Most built-in object implement only `toString()`. From the algorithm string conversion is much more widely used
// try getting the type of a browser window object?
174
+
window.toStringPlain= s;
155
175
176
+
alert( window.toStringPlain() ); // [object Window] <-- it works!
177
+
```
156
178
179
+
Please note that different objects usually have their own `toString`. As we've seen above, the `toString` of `Array` returns a list of items. So we need to use exactly the `toString` of a plain object -- `{}.toString`.
157
180
181
+
To call it in the right context, we copy it into a variable `s` -- in Javascript functions are not hardwired to objects, even built-in ones, so we do it -- and then assign as a property to another object `arr.toStringPlain` (not to override `arr.toString`). That's called *method borrowing*.
158
182
159
-
The similar thing with the method `valueOf`. It is called when the object is converted to a number.
183
+
Actually, we could evade all complexities using [call](info:object-methods#call-apply)to pass `this`:
0 commit comments