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
Специальные значения `null` и `undefined` стоят особняком. У них нет, объектов-оберток, так что методы и свойства им недоступны. Также у них нет соответствующих прототипов.
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.
122
+
В течение процесса разработки, у нас могут быть идеи о новых встроенных методах, которые нам хотелось бы иметь, и можем испытать искушение добавить их во встроенные прототипы. Но в целом, это плохая идея.
123
123
124
124
```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 will be overwriting the other.
125
+
Прототипы глобальны, и очень легко получить конфликты. Если две библиотеки добавляют методы `String.prototype.show`, то одна из них перепишет другой.
126
126
127
-
So, generally, modifying a native prototype is considered a bad idea.
127
+
Так что, в целом, изменение встроенных прототипов считается плохой идей.
128
128
```
129
129
130
-
**In modern programming, there is only one case where modifying native prototypes is approved. That's polyfilling.**
130
+
**В современном программировании есть только один случай, в котором одобряется изменение встроенных прототипов. Это создание полифилов.
131
131
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
+
Полифил - это термин, который означает замену метода, существуего в спецификации JavaScript, но еще не поддерживается текущим движком JavaScript.
133
133
134
-
Then we may implement it manually and populate the built-in prototype with it.
134
+
Тогда мы можем реализовать его сами и добавить его во встроенный прототип.
135
135
136
-
For instance:
136
+
Например:
137
137
138
138
```js run
139
-
if (!String.prototype.repeat) { //if there's no such method
140
-
//add it to the prototype
139
+
if (!String.prototype.repeat) { //Если такого метода нет
140
+
//добавляем его в прототип
141
141
142
142
String.prototype.repeat=function(n) {
143
-
//repeat the string n times
143
+
//повторить строку n раз
144
144
145
-
//actually, the code should be a little bit more complex than that
146
-
// (the full algorithm is in the specification)
147
-
//but even an imperfect polyfill is often considered good enough
145
+
//на самом деле код должен быть немного более сложным
146
+
// (полный алгоритм можно найти в спецификации)
147
+
//но даже неполный полифил часто считается достаточно хорошим
0 commit comments