From 074785cea106179cb3305637055ab0a009ca74f2 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:42:52 -0500 Subject: initial commit --- .../global_objects/string/codepointat/index.html | 174 +++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 files/ru/web/javascript/reference/global_objects/string/codepointat/index.html (limited to 'files/ru/web/javascript/reference/global_objects/string/codepointat') diff --git a/files/ru/web/javascript/reference/global_objects/string/codepointat/index.html b/files/ru/web/javascript/reference/global_objects/string/codepointat/index.html new file mode 100644 index 0000000000..db2b8a4e19 --- /dev/null +++ b/files/ru/web/javascript/reference/global_objects/string/codepointat/index.html @@ -0,0 +1,174 @@ +--- +title: String.prototype.codePointAt() +slug: Web/JavaScript/Reference/Global_Objects/String/codePointAt +tags: + - ECMAScript6 + - Experimental + - Expérimental(2) + - JavaScript + - Method + - Prototype + - Reference + - Référence(2) + - String +translation_of: Web/JavaScript/Reference/Global_Objects/String/codePointAt +--- +
{{JSRef("Global_Objects", "String")}}
+ +

Сводка

+ +

Метод codePointAt() возвращает неотрицательное целое число, являющееся закодированным в UTF-16 значением кодовой точки.

+ +

Синтаксис

+ +
str.codePointAt(pos)
+ +

Параметры

+ +
+
pos
+
Позиция элемента в строке, чья кодовоя точка возвращается функцией.
+
+ +

Описание

+ +

Если на указанной позиции нет элементов, будет возвращено значение {{jsxref("Global_Objects/undefined", "undefined")}}. Если суррогатная пара UTF-16 не начинается в позиции pos, будет возвращено кодовое значение в позиции pos.

+ +

Примеры

+ +

Пример: использование метода codePointAt()

+ +
'ABC'.codePointAt(1);          // 66
+'\uD800\uDC00'.codePointAt(0); // 65536
+
+'XYZ'.codePointAt(42); // undefined
+
+ +

Полифилл

+ +

Следующий полифилл расширяет прототип строки определённой в ECMAScript 6 функцией codePointAt(), если браузер не имеет её родной поддержки.

+ +
/*! http://mths.be/codepointat v0.1.0 от @mathias */
+if (!String.prototype.codePointAt) {
+  (function() {
+    'use strict'; // необходимо для поддержки методов `apply`/`call` с `undefined`/`null`
+    var codePointAt = function(position) {
+      if (this == null) {
+        throw TypeError();
+      }
+      var string = String(this);
+      var size = string.length;
+      // `ToInteger`
+      var index = position ? Number(position) : 0;
+      if (index != index) { // лучше, чем `isNaN`
+        index = 0;
+      }
+      // Проверяем выход индекса за границы строки
+      if (index < 0 || index >= size) {
+        return undefined;
+      }
+      // Получаем первое кодовое значение
+      var first = string.charCodeAt(index);
+      var second;
+      if ( // проверяем, не начинает ли оно суррогатную пару
+        first >= 0xD800 && first <= 0xDBFF && // старшая часть суррогатной пары
+        size > index + 1 // следующее кодовое значение
+      ) {
+        second = string.charCodeAt(index + 1);
+        if (second >= 0xDC00 && second <= 0xDFFF) { // младшая часть суррогатной пары
+          // http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
+          return (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
+        }
+      }
+      return first;
+    };
+    if (Object.defineProperty) {
+      Object.defineProperty(String.prototype, 'codePointAt', {
+        'value': codePointAt,
+        'configurable': true,
+        'writable': true
+      });
+    } else {
+      String.prototype.codePointAt = codePointAt;
+    }
+  }());
+}
+
+ +

Спецификации

+ + + + + + + + + + + + + + +
СпецификацияСтатусКомментарии
{{SpecName('ES6', '#sec-string.prototype.codepointat', 'String.prototype.codePointAt')}}{{Spec2('ES6')}}Изначальное определение.
+ +

Совместимость с браузерами

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
ВозможностьChromeFirefox (Gecko)Internet ExplorerOperaSafari
Базовая поддержка{{CompatChrome("41")}}{{CompatGeckoDesktop("29")}}11 на Windows 10 Preview{{CompatNo}}{{CompatNo}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
ВозможностьAndroidChrome для AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Базовая поддержка{{CompatNo}}{{CompatNo}}{{CompatGeckoMobile("29")}}{{CompatNo}}{{CompatNo}}{{CompatNo}}
+
+ +

Смотрите также

+ + -- cgit v1.2.3-54-g00ecf