Qiita Conference 2025

桜庭洋之 (@zaru)

一緒に働きたくなるプログラマの思想

0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

#プログラミング #Javascript # バグネタ

Posted at

バグを生みそう・生んだもの。

なんかエンジン(Chrome)や言語のバグのような気もするけど。。。
とりあえずこれはChrome上だけど、多分Node.jsとかでも同じになる気がする。
fillメソッドの引数はforeachとかみたいに毎回実行されるのではなく、最初に用意して使いまわすってことっぽい。
(配列内は同一オブジェクトを参照してる。)

スクリーンショット 2025-04-16 053056.png

ちなみにそういう初期化をしようとして更に気づいたのはこれ。
何故かmapメソッドは機能しない。
fillメソッドは「new Array(5).fill~」としても使える。

スクリーンショット 2025-04-16 053822.png

既知だと思うけど、JSの要素が無いことと中身がないことは別のこと。
長さ3のaという配列で、delete a[1]とするとa[1]はundefinedになるが、その要素は存在し、中身が無いだけ。
(要素が存在するという根拠は、aの長さは3のままになるので。)
ただ、a[3]もundefinedになるから、挙動が曖昧な気もちょっとするけど。。。
中身がなくても要素があればfillもmapも機能すると思うんだけどね~。
ま、敢えてなら、mapは「元を利用して」だから、中身も無いとって解釈もできるけど。

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
qiita21409102

@qiita21409102(K.E.Y.)

寄付をお願いします(^^; https://ko-fi.com/kofi8102

Comments

ibyrjbf
@ibyrjbf

なんかエンジン(Chrome)や言語のバグのような気もするけど。。。

本当にバグでしょうか。
仕様ではないのですか?

0

Let's comment your feelings that are more than good

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details

Being held Article posting campaign

0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Login to continue?

Login or Sign up with social account

Login or Sign up with your email address