Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StringとArrayの.atメソッドの対応 #1392

Merged
merged 10 commits into from
Feb 12, 2022
Merged

StringとArrayの.atメソッドの対応 #1392

merged 10 commits into from
Feb 12, 2022

Conversation

azu
Copy link
Collaborator

@azu azu commented Feb 11, 2022

Array.prototype.at

https://jsprimer.net/basic/array/

配列の末尾の要素へアクセスするには、array[array.length - 1]というlengthプロパティを使う必要がありました。
arrayを2回書く必要があるなど、末尾の要素へのアクセスは少し手間が必要になっていました。

この問題を解決するためES2022では、相対的なインデックスの値を指定して配列の要素へアクセスできるArray.prototype.atメソッドが追加されました。
Arrayのatメソッドは、配列[インデックス]とよく似ていますが、引数には相対的なインデックスの値を引数として渡せます。.at(0)なら配列の先頭の要素へ、.at(-1)なら配列の末尾の要素へアクセスできます。

const array = ["a", "b", "c"];
// 先頭の要素にアクセス
console.log(array.at(0)); // => "a"
console.log(array[0]); // => "a"
// 後ろから1つ目の要素にアクセス
console.log(array.at(-1)); // => "c"
console.log(array[array.length - 1]); // => "c"

String.prototype.at

https://jsprimer.net/basic/string/

配列と同じく文字列にも相対的なインデックスの値を指定して文字へアクセスできるString.prototype.atが追加されました。

const str = "文字列";
console.log(str.at(0)); // => "文"
console.log(str.at(1)); // => "字"
console.log(str.at(2)); // => "列"
console.log(str.at(-1)); // => "列"

変更点

fix #1367

cc @mongolyy

@azu azu added the Lang: ES2022 ECMAScript 2022 label Feb 11, 2022
@bot-user
Copy link

bot-user commented Feb 11, 2022

✔️ Deploy Preview for js-primer ready!

🔨 Explore the source changes: 5e376ba

🔍 Inspect the deploy log: https://app.netlify.com/sites/js-primer/deploys/6207b7c54d43f30007ec56cd

😎 Browse the preview: https://deploy-preview-1392--js-primer.netlify.app

console.log(array[array.length - 1]); // => "c"
```

`配列[インデックス]`のインデックスに`-1`を指定すると、配列オブジェクトの`"-1"`というプロパティ名へのアクセスとなります。
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

array[-1] の話 微妙に唐突な感じがして入らないかもしれない…

@@ -1,21 +0,0 @@
class MyArray extends Array {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class/README.mdの方で同じコードがあってそっちでテストできてるので入らなそうだった。

Copy link
Contributor

@mongolyy mongolyy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

お疲れさまです!ありがとうございます!
すごく細かいところですが、一点だけコメントいたしました!


この問題を解決するためES2022では、相対的なインデックスの値を指定して配列の要素へアクセスできる`Array.prototype.at`メソッドが追加されました。
Arrayの`at`メソッドは、`配列[インデックス]`とよく似ていますが、引数には相対的なインデックスの値を引数として渡せます。
`.at(0)`なら配列の先頭の要素へ、`.at(-1)`なら配列の末尾の要素へアクセスできます。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.at(-2)などの挙動を説明するためにも、String.prototype.at と同様に

`-1`のようにマイナスのインデックスを渡した場合は、末尾から数えた位置の文字へアクセスできます。

の記述を加えてもいいかもしれないと、思いました。
(azuさんがissueで言及されているように、.at(-1) しか使われないようにも思えますが、念の為 😅 )

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます!

`.at(0)`や`.at(1)`などのように0以上のインデックスを渡した場合は、`配列[インデックス]`と同じく指定した位置の要素へアクセスできます。
一方で、`.at(-1)`のようにマイナスのインデックスを渡した場合は、末尾から数えた位置の要素へアクセスできます。

という感じにしました。

@azu azu merged commit ec04995 into asciidwango:master Feb 12, 2022
@azu azu deleted the feature/1367 branch February 12, 2022 14:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Lang: ES2022 ECMAScript 2022
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ES2022: StringとArrayの .at()
3 participants