| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt | [Back] [Original] |
Get to know MDN better
Esta pgina foi traduzida do ingls pela comunidade. Saiba mais e junte-se comunidade MDN Web Docs.
This feature is well established and works across many devices and browser versions. Its been available across browsers since setembro de 2015.
O mtodo codePointAt() retorna um nmero inteiro no negativo que o valor do ponto de cdigo Unicode.
const icons = "";
console.log(icons.codePointAt(1));
// Expected output: "9733"
str.codePointAt(pos)
posA posio de um elemento em uma string a partir do qual retorna o valor do ponto de cdigo.
Um nmero que representa o valor do ponto de cdigo do caractere na pos fornecida. Se no houver nenhum elemento na pos, undefined retornado.
Se no houver nenhum elemento na posio especificada, retornado o valor de undefined. Se nenhum par substituto UTF-16 comear na pos, a unidade de cdigo na pos ser retornada.
O seguinte cdigo cria no objeto global String a funo codePointAt() conforme especificado em ECMAScript 2015 para navegadores sem suporte nativo:
/*! 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 its 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;
}
})();
}
codePointAt()"ABC".codePointAt(1); // retorna 66
"\uD800\uDC00".codePointAt(0); // retorna 65536
"XYZ".codePointAt(42); // retorna undefined
codePointAt()for (let codePoint of "\ud83d\udc0e\ud83d\udc71\u2764") {
console.log(codePoint.codePointAt(0).toString(16));
}
// retorna '1f40e', '1f471', '2764'
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-string.prototype.codepointat |
String.fromCodePoint()String.fromCharCode()String.prototype.charCodeAt()String.prototype.charAt()This page was last modified on 17 de fev. de 2025 by MDN contributors.
StringString.prototype.anchor()at()String.prototype.big()String.prototype.blink()String.prototype.bold()String.prototype.charAt()String.prototype.charCodeAt()String.prototype.codePointAt()String.prototype.concat()String.prototype.endsWith()String.prototype.fixed()String.prototype.fontcolor()String.prototype.fontsize()String.prototype.includes()String.prototype.indexOf()isWellFormed()String.prototype.italics()String.prototype.lastIndexOf()String.prototype.link()String.prototype.localeCompare()String.prototype.match()String.prototype.matchAll()String.prototype.normalize()String.prototype.padEnd()String.prototype.padStart()String.prototype.repeat()String.prototype.replace()String.prototype.replaceAll()String.prototype.search()String.prototype.slice()String.prototype.small()String.prototype.split()String.prototype.startsWith()String.prototype.strike()String.prototype.sub()String.prototype.substr()String.prototype.substring()String.prototype.sup()String.prototype.toLocaleLowerCase()String.prototype.toLocaleUpperCase()String.prototype.toLowerCase()String.prototype.toString()String.prototype.toUpperCase()toWellFormed()String.prototype.trim()String.prototype.trimEnd()String.prototype.trimStart()String.prototype.valueOf()String.prototype[@@iterator]()Object/FunctionObject.prototype.__defineGetter__()Object.prototype.__defineSetter__()Object.prototype.__lookupGetter__()Object.prototype.__lookupSetter__()Object.prototype.hasOwnProperty()Object.prototype.isPrototypeOf()Object.prototype.propertyIsEnumerable()Object.prototype.toLocaleString()Object.prototype.toString()Object.prototype.valueOf()Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |