Skip to content

Commit 187c3d0

Browse files
committed
Translate "Bonus: Object toString for the type" part
1 parent eef06a6 commit 187c3d0

1 file changed

Lines changed: 17 additions & 17 deletions

File tree

1-js/09-classes/06-instanceof/article.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -117,45 +117,45 @@ alert( rabbit instanceof Rabbit ); // false
117117

118118
Это одна из причин избегать изменения `prototype`. Просто для безопасности.
119119

120-
## Bonus: Object toString for the type
120+
## Бонус: Object.toString для типизации
121121

122-
We already know that plain objects are converted to string as `[object Object]`:
122+
Мы уже знаем, что обычные объекты преобразуется к строке как `[object Object]`:
123123

124124
```js run
125125
let obj = {};
126126
127127
alert(obj); // [object Object]
128-
alert(obj.toString()); // the same
128+
alert(obj.toString()); // тоже самое
129129
```
130130

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

133-
Sounds strange? Indeed. Let's demystify.
133+
Звучит странно? Так и есть. Давайте развеем мистику.
134134

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` может бы позаимствован у объекта и вызван в контексте любого другого значения. И результат зависит от типа этого значения.
136136

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+
- ...и т.д. (поведение настраивается).
143143

144-
Let's demonstrate:
144+
Давайте продемонстрируем:
145145

146146
```js run
147-
// copy toString method into a variable for convenience
147+
// скопируем метод toString в переменную для удобства
148148
let objectToString = Object.prototype.toString;
149149
150-
// what type is this?
150+
// Какой это тип?
151151
let arr = [];
152152
153153
alert( objectToString.call(arr) ); // [object Array]
154154
```
155155

156-
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`.
157157

158-
Internally, the `toString` algorithm examines `this` and returns the corresponding result. More examples:
158+
Внутри, алгоритм метода `toString` анализирует контекст вызова `this` и возвращает соответствующий результат. Больше примеров:
159159

160160
```js run
161161
let s = Object.prototype.toString;

0 commit comments

Comments
 (0)