Movatterモバイル変換


[0]ホーム

URL:


  1. Tecnologia Web para desenvolvedores
  2. JavaScript
  3. Referência JavaScript
  4. Objetos Globais
  5. String
  6. String.prototype.codePointAt()

Esta página foi traduzida do inglês pela comunidade.Saiba mais e junte-se à comunidade MDN Web Docs.

View in EnglishAlways switch to English

String.prototype.codePointAt()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨setembro de 2015⁩.

O métodocodePointAt() retorna um número inteiro não negativo que é o valor do ponto de código Unicode.

Experimente

const icons = "☃★♲";console.log(icons.codePointAt(1));// Expected output: "9733"

Sintaxe

str.codePointAt(pos)

Parâmetros

pos

A posição de um elemento em uma string a partir do qual retorna o valor do ponto de código.

Valor retornado

Um número que representa o valor do ponto de código do caractere napos fornecida. Se não houver nenhum elemento napos,undefined é retornado.

Descrição

Se não houver nenhum elemento na posição especificada, é retornado o valor deundefined. Se nenhum par substituto UTF-16 começar napos, a unidade de código napos será retornada.

Polyfill

O seguinte código cria no objeto global String a funçãocodePointAt() conforme especificado em ECMAScript 2015 para navegadores sem suporte nativo:

js
/*! https://mths.be/codepointat v0.2.0 by @mathias */if (!String.prototype.codePointAt) {  (function () {    "use strict"; // needed to support `apply`/`call` with `undefined`/`null`    var defineProperty = (function () {      // IE 8 only supports `Object.defineProperty` on DOM elements      try {        var object = {};        var $defineProperty = Object.defineProperty;        var result = $defineProperty(object, object, object) && $defineProperty;      } catch (error) {}      return result;    })();    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) {        // better `isNaN`        index = 0;      }      // Account for out-of-bounds indices:      if (index < 0 || index >= size) {        return undefined;      }      // Get the first code unit      var first = string.charCodeAt(index);      var second;      if (        // check if it’s the start of a surrogate pair        first >= 0xd800 &&        first <= 0xdbff && // high surrogate        size > index + 1 // there is a next code unit      ) {        second = string.charCodeAt(index + 1);        if (second >= 0xdc00 && second <= 0xdfff) {          // low surrogate          // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae          return (first - 0xd800) * 0x400 + second - 0xdc00 + 0x10000;        }      }      return first;    };    if (defineProperty) {      defineProperty(String.prototype, "codePointAt", {        value: codePointAt,        configurable: true,        writable: true,      });    } else {      String.prototype.codePointAt = codePointAt;    }  })();}

Exemplos

UsandocodePointAt()

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

Criando um loop comcodePointAt()

js
for (let codePoint of "\ud83d\udc0e\ud83d\udc71\u2764") {  console.log(codePoint.codePointAt(0).toString(16));}// retorna '1f40e', '1f471', '2764'

Especificações

Specification
ECMAScript® 2026 Language Specification
# sec-string.prototype.codepointat

Compatibilidade com navegadores

Veja também

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp