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
Это одна из причин избегать изменения `prototype`. Просто для безопасности.
119
119
120
-
## Bonus:ObjecttoString for the type
120
+
## Бонус:Object.toStringдля типизации
121
121
122
-
We already know that plain objects are converted to string as`[object Object]`:
122
+
Мы уже знаем, что обычные объекты преобразуется к строке как`[object Object]`:
123
123
124
124
```js run
125
125
let obj = {};
126
126
127
127
alert(obj); // [object Object]
128
-
alert(obj.toString()); // the same
128
+
alert(obj.toString()); // тоже самое
129
129
```
130
130
131
-
That's their implementation of `toString`. But there's a hidden feature that makes `toString`actually much more powerful than that. We can use it as an extended`typeof`and an alternative for`instanceof`.
131
+
Так работает реализация метода`toString` у объектов. Но у `toString`имеются скрытые возможности, которые делают метод гораздо более мощным. Мы можем использовать его как расширенную версию`typeof`и как альтернативу`instanceof`.
132
132
133
-
Sounds strange?Indeed. Let's demystify.
133
+
Звучит странно? Так и есть. Давайте развеем мистику.
134
134
135
-
By [specification](https://tc39.github.io/ecma262/#sec-object.prototype.tostring), the built-in `toString` can be extracted from the object and executed in the context of any other value. And its result depends on that value.
135
+
Согласно [спецификации](https://tc39.github.io/ecma262/#sec-object.prototype.tostring) встроенный метод `toString` может бы позаимствован у объекта и вызван в контексте любого другого значения. И результат зависит от типа этого значения.
136
136
137
-
- For a number, it will be `[object Number]`
138
-
- For a boolean, it will be `[object Boolean]`
139
-
- For `null`: `[object Null]`
140
-
- For `undefined`: `[object Undefined]`
141
-
- For arrays: `[object Array]`
142
-
- ...etc (customizable).
137
+
-Для числа, это будет`[object Number]`
138
+
-Для булева типа, это будет`[object Boolean]`
139
+
-Для`null`:`[object Null]`
140
+
-Для`undefined`:`[object Undefined]`
141
+
-Для массивов:`[object Array]`
142
+
-...и т.д. (поведение настраивается).
143
143
144
-
Let's demonstrate:
144
+
Давайте продемонстрируем:
145
145
146
146
```js run
147
-
// copy toString method into a variable for convenience
147
+
// скопируем метод toString в переменную для удобства
Here we used [call](mdn:js/function/call) as described in the chapter [](info:call-apply-decorators) to execute the function `objectToString` in the context `this=arr`.
156
+
В примере мы использовали [call](mdn:js/function/call), как описано в главе [](info:call-apply-decorators), чтобы выполнить функцию `objectToString` в контексте `this=arr`.
157
157
158
-
Internally, the `toString` algorithm examines `this` and returns the corresponding result. More examples:
158
+
Внутри, алгоритм метода `toString` анализирует контекст вызова `this` и возвращает соответствующий результат. Больше примеров:
0 commit comments