[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt [Back]  [Original]

String.prototype.codePointAt() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

String.prototype.codePointAt()

Baseline
Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 ..

codePointAt() , UTF-16 .

In this article

str.codePointAt(pos)

pos

, .

, undefined. UTF-16 pos, pos.

: codePointAt()

js
"ABC".codePointAt(1); // 66
"\uD800\uDC00".codePointAt(0); // 65536

"XYZ".codePointAt(42); // undefined

ECMAScript 6 codePointAt(), .

js
/*! 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;
    }
  })();
}

Specification
ECMAScript 2027 LanguageSpecification
# sec-string.prototype.codepointat


Web Proxy Viewer  |  New URL  |  Original Page