-
-
Notifications
You must be signed in to change notification settings - Fork 224
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
StringとArrayの.atメソッドの対応 #1392
Conversation
✔️ 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"`というプロパティ名へのアクセスとなります。 |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class/README.mdの方で同じコードがあってそっちでテストできてるので入らなそうだった。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
お疲れさまです!ありがとうございます!
すごく細かいところですが、一点だけコメントいたしました!
source/basic/array/README.md
Outdated
|
||
この問題を解決するためES2022では、相対的なインデックスの値を指定して配列の要素へアクセスできる`Array.prototype.at`メソッドが追加されました。 | ||
Arrayの`at`メソッドは、`配列[インデックス]`とよく似ていますが、引数には相対的なインデックスの値を引数として渡せます。 | ||
`.at(0)`なら配列の先頭の要素へ、`.at(-1)`なら配列の末尾の要素へアクセスできます。 |
There was a problem hiding this comment.
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)
しか使われないようにも思えますが、念の為 😅 )
There was a problem hiding this comment.
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)`のようにマイナスのインデックスを渡した場合は、末尾から数えた位置の要素へアクセスできます。
という感じにしました。
Array.prototype.at
配列の末尾の要素へアクセスするには、
array[array.length - 1]
というlength
プロパティを使う必要がありました。array
を2回書く必要があるなど、末尾の要素へのアクセスは少し手間が必要になっていました。この問題を解決するためES2022では、相対的なインデックスの値を指定して配列の要素へアクセスできる
Array.prototype.at
メソッドが追加されました。Arrayの
at
メソッドは、配列[インデックス]
とよく似ていますが、引数には相対的なインデックスの値を引数として渡せます。.at(0)
なら配列の先頭の要素へ、.at(-1)
なら配列の末尾の要素へアクセスできます。String.prototype.at
配列と同じく文字列にも相対的なインデックスの値を指定して文字へアクセスできる
String.prototype.at
が追加されました。変更点
Array.prototype.at
の追加String.prototype.at
の追加get first()
とget last()
にat
メソッドを使うように変更fix #1367
cc @mongolyy