-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
translation(js): Web/JavaScript/Reference/Global_Objects/String/at (#170
) * translation(js): Web/JavaScript/Reference/Global_Objects/String/at * Apply suggestions from code review Co-authored-by: Vitalii Perehonchuk <[email protected]> Co-authored-by: Vitalii Perehonchuk <[email protected]>
- Loading branch information
1 parent
13887a8
commit 16d2c41
Showing
1 changed file
with
95 additions
and
0 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
files/uk/web/javascript/reference/global_objects/string/at/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
--- | ||
title: String.prototype.at() | ||
slug: Web/JavaScript/Reference/Global_Objects/String/at | ||
tags: | ||
- String | ||
- JavaScript | ||
- Method | ||
- Prototype | ||
- Reference | ||
- polyfill | ||
- at | ||
- Experimental | ||
browser-compat: javascript.builtins.String.at | ||
--- | ||
{{JSRef}} | ||
|
||
Метод **`at()`** приймає ціле число і повертає новий {{jsxref('String', 'рядок')}}, що складається з однієї кодової одиниці UTF-16, розміщеної за вказаною позицією. Цей метод приймає як додатні, так і від'ємні цілі числа. Від'ємні числа позначають номер позиції у зворотному напрямку, від останнього символу рядка. | ||
|
||
{{EmbedInteractiveExample("pages/js/string-at.html")}} | ||
|
||
## Синтаксис | ||
|
||
```js | ||
at(index) | ||
``` | ||
|
||
### Параметри | ||
|
||
- `index` | ||
- : Індекс (позиція) символу рядка, який потрібно отримати. Дає змогу також обирати позицію з кінця рядка, якщо передати від'ємне значення. Іншими словами, якщо вжито від'ємне число, цільовий символ буде знайдено шляхом перебирання позицій у зворотному напрямку, з кінця сторінки. | ||
|
||
### Повернене значення | ||
|
||
{{jsxref('String', 'Рядок')}}, який містить єдину кодову одиницю UTF-16, взяту за вказаною позицією. Повертає {{jsxref('undefined')}}, якщо за переданим індексом нічого не знайдено. | ||
|
||
## Приклади | ||
|
||
### Повернення останнього символу рядка | ||
|
||
Наступний приклад демонструє функцію, яка повертає останній символ, знайдений у вказаному рядку. | ||
|
||
```js | ||
// Функція, яка повертає останній символ переданого рядка | ||
function returnLast(arr) { | ||
return arr.at(-1); | ||
} | ||
|
||
let invoiceRef = 'мійІнвойс01'; | ||
|
||
console.log( returnLast(invoiceRef) ); | ||
// Друкує: '1' | ||
|
||
invoiceRef = 'мійІнвойс02'; | ||
|
||
console.log( returnLast(invoiceRef) ); | ||
// Друкує: '2' | ||
``` | ||
|
||
### Порівняння методів | ||
|
||
Нижче наведено порівняння різних способів отримання передостаннього символу {{jsxref('String', 'рядка')}}. Позаяк всі наведені далі методи — коректні, з-поміж них виділяється лаконічність і чіткість саме методу `at()`. | ||
|
||
```js | ||
const myString = 'Всякий зелений автобус їде швидко.'; | ||
|
||
// Використання властивості "length" і методу charAt() | ||
const lengthWay = myString.charAt(myString.length-2); | ||
console.log(lengthWay); // Друкує: 'о' | ||
|
||
// За допомогою методу slice() | ||
const sliceWay = myString.slice(-2, -1); | ||
console.log(sliceWay); // Друкує: 'о' | ||
|
||
// Шляхом застосування методу at() | ||
const atWay = myString.at(-2); | ||
console.log(atWay); // Друкує: 'о' | ||
``` | ||
|
||
## Специфікації | ||
|
||
{{Specifications}} | ||
|
||
## Сумісність із браузерами | ||
|
||
{{Compat}} | ||
|
||
## Дивіться також | ||
|
||
- Поліфіл методу `String.prototype.at` також доступний у [`core-js`](https://github.com/zloirock/core-js#ecmascript-string-and-regexp) | ||
- [Поліфіл методу "at()"](https://github.com/tc39/proposal-relative-indexing-method#polyfill). | ||
- {{jsxref("String.prototype.indexOf()")}} | ||
- {{jsxref("String.prototype.lastIndexOf()")}} | ||
- {{jsxref("String.prototype.charCodeAt()")}} | ||
- {{jsxref("String.prototype.codePointAt()")}} | ||
- {{jsxref("String.prototype.split()")}} |