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

Fix: Array.from()[...]に変更 #203

Open
manabuyasuda opened this issue May 2, 2019 · 2 comments
Open

Fix: Array.from()[...]に変更 #203

manabuyasuda opened this issue May 2, 2019 · 2 comments
Labels
bug バグの報告と修正。修正したらクローズする。

Comments

@manabuyasuda
Copy link
Owner

4.4 繰り返し可能なオブジェクトを配列に変換する場合はArray.fromの代わりにスプレッド構文...を使用すること。

https://github.com/mitsuruog/javascript-style-guide#arrays--from-iterable

@manabuyasuda manabuyasuda added the bug バグの報告と修正。修正したらクローズする。 label May 2, 2019
@manabuyasuda
Copy link
Owner Author

ただし、

4.6 繰り返し可能なオブジェクト(iterables)へのマッピングにはスプレッド構文...の代わりにArray.fromを使用すること。理由は中間配列の作成を防ぐためです。

// bad
const baz = [...foo].map(bar);

// good
const baz = Array.from(foo, bar);

とあるので、単純に[...]に変更するのではなく、map()filter()などで新しい配列を生成する場合はArray.from()にすると推測される。

@manabuyasuda
Copy link
Owner Author

stBreadcrumbの以下の箇所をレストパラメータにしてもいいかなと思った。

    [...link].forEach(item => {
      const href = item.getAttribute('href').replace(/index\.html$/, '');
      const isMatchCurrentPage = href === path;

      if (isMatchCurrentPage) {
        item.setAttribute('aria-current', currentValue);
        item.setAttribute('tabindex', -1);
      }
    });

でも、以下のような関数がスコープごとに作られてしまう。
これが「中間配列の作成」なんだろうか?

_toConsumableArray(arr);
_nonIterableSpread();
_iterableToArray(iter);
_arrayWithoutHoles(arr);

Array.from() メソッドは、配列風オブジェクトや反復可能オブジェクトから、新しい、浅いコピーの Array インスタンスを生成します。

rest parameters とは、不特定多数の引数を配列として受け取る構文です。

MDNを見ていても、レストパラメータよりArray.from()の方が意図が明確な感じがする。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug バグの報告と修正。修正したらクローズする。
Projects
None yet
Development

No branches or pull requests

1 participant