Skip to content

Commit 75cdcb4

Browse files
committed
ok
1 parent 14d6875 commit 75cdcb4

File tree

3 files changed

+47
-31
lines changed

3 files changed

+47
-31
lines changed

1-js/2-first-steps/07-types/article.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ Here we have a variable `key` which contains the property name, probably evaluat
234234

235235
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.
236236

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 of things. 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 of things. The term *plain objects* or just `Object` (capital first) is used to represent "basic" objects, the ones we create with `{ ... }`.
238238

239239
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.
240240

@@ -385,7 +385,7 @@ There are 7 basic types in JavaScript.
385385
- `null` for unknown values.
386386
- `undefined` for unassigned values.
387387
- `symbol` for unique identifiers.
388-
- `object` for more complex data structures (there exist many, we saw arrays).
388+
- `object` for more complex data structures.
389389

390390
The `typeof` operator allows to see which type is stored in the variable.
391391

1-js/2-first-steps/20-object-methods/article.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ Any operation on the Reference Type immediately "resolves" it:
264264

265265
So any operation on the result of dot `'.'` except a direct call discards `this`.
266266

267-
## Explicit "this" with "call/apply"
267+
## Explicit "this" with "call/apply" [#call-apply]
268268

269269
We can call a function explicitly providing the value of `"this"`.
270270

@@ -335,7 +335,7 @@ So the use of `apply` over `call` is mainly a metter of personal preference. And
335335

336336
There's still a way to bind "this" to a function.
337337

338-
[todo] ????
338+
[todo] migrate bind here????
339339

340340

341341
## Summary

1-js/2-first-steps/21-object-tostring-valueof/article.md

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ In most projects though, only `toString()` is used, because objects are printed
9494

9595
If only `toString()` is implemented, then both string and numeric conversions use it.
9696

97-
## Examples for built-ins
97+
## Array example
9898

9999
Let's check a few examples to finally get the whole picture.
100100

@@ -118,61 +118,77 @@ alert( '1,2' + 1 ); // '1,21'
118118

119119
Now the addition has the first operand -- a string, so it converts the second one to a string also. Hence the result.
120120

121-
Now with a plain object:
121+
## Object, toString for the type
122+
123+
With plain objects it's much more interesting.
124+
125+
An object has both `valueOf()` and `toString()`, but for plain objects `valueOf()` returns the object itself:
122126

123127
```js run
124-
alert( +{} ); // NaN
125-
alert( {} + {} ); // [object Object][object Object]
126-
```
128+
let obj = { };
127129

128-
Plain objects actually have both `toString()` and `valueOf()`:
130+
alert( obj === obj.valueOf() ); // true, valueOf returns the object itself
131+
```
129132

130-
................TODO ALG OF OBJECT TOSTRING
133+
Because `ToPrimitive` ignores `valueOf` if it returns an object, here we can assume that `valueOf` does not exist at all.
131134

132-
The result of these operations should be somewhat obvious now.
135+
Now `toString`.
133136

137+
From the first sight it's obvious:
134138

139+
```js run
140+
let obj = { };
135141

142+
alert( obj ); // [object Object]
143+
```
136144

145+
But it's much more powerful than that.
137146

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

139-
## [[Class]]
149+
The algorithm of the `toString()` for plain objects looks like this:
140150

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

143-
But there's a semi-hidden way to access the right class.
144155

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`).
146157

158+
All we need to do to get the type of an `obj` -- is to call plain object `toString` passing `this = obj`.
147159

160+
We can do it like this:
148161

162+
```js run
163+
let s = {}.toString; // copy toString of Object to a variable
149164

150-
Here are some built-in objects
165+
// what type is this?
166+
let arr = [];
151167

152-
Most built-in object implement only `toString()`. From the algorithm string conversion is much more widely used
168+
// copy Object toString to it:
169+
arr.toStringPlain = s;
153170

171+
alert( arr.toStringPlain() ); // [object Array] <-- right!
154172

173+
// try getting the type of a browser window object?
174+
window.toStringPlain = s;
155175

176+
alert( window.toStringPlain() ); // [object Window] <-- it works!
177+
```
156178

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

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

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`:
160184

161185
```js run
162-
let room = {
163-
number: 777,
164-
165-
valueOf() {
166-
return this.number;
167-
},
168-
};
186+
let arr = [];
169187

170-
alert( +room ); // 777, valueOf is called
188+
alert( {}.toString.call(arr) ); // [object Array]
189+
alert( {}.toString.call(window) ); // [object Window]
171190
```
172191

173-
What really sounds strange -- is the name of the method. Why is it called "valueOf", why not "toNumber"?
174-
175-
The reason is that `valueOf` is used by default to convert an object to a primitive for operations where a primitive value is required.
176-
192+
Here we do the same in one line: get the `toString` of a plain object and call it with the right `this` to get its type.
177193

178194

0 commit comments

Comments
 (0)