Giter Club home page Giter Club logo

Comments (12)

azu avatar azu commented on August 19, 2024 2

普通に書くとforとかはホントMDNのままになってしまうので、もう少し反復処理をArrayメソッドの話によせるのが良さそうな気がする。

を見ていてそんな事を思った。

これをやるには関数とメソッドについて事前に触れる必要がある(全体的にユースケースを出していくなら関数とかは早めに紹介した方がいい)
後、色々混ざって順番が前後する感じだけど、リファレンス的なものを読みたいならMDNがあるのでそっちを見てもらったほうがいい気がする。
なので、この本だともっと実際のユースケースによった方がよさそうかなと思った。

for文はこういう書き方ができます。
=>
for文はこういう書き方で、ベタに手続き的にfor文を書くことはなくて普通は関数で抽象化するよね。
さらにfor文よりArray#reduceとかで実装するとletとかも不要になっていくよね。
(蛇足: Arrayのメソッドはfor文で実装されていることが多いので、そう考えると速度はどっちが早いかは明らか。でもより安全なコードを書くことができるよ的な)

みたいになっていたほうがいいのかなーと思った。

from js-primer.

azu avatar azu commented on August 19, 2024 1

この章は全体的に for と Arrayの対比になる感じがする。

  • for <=> forEach
  • for + break <=> Array#some
  • for + continue <=> Array#filter
  • while <=> なし
  • for of <=> ... spread operator + Array

になるのかなー。
whileの例が難しいというか、もっとも原始的なループなんだなと思った。
基本シングルスレッドなJavaScriptで重たい処理は避けるから、
whileでbusyループはしないんだよな。今ならGeneratorがあるし。

whileの例があんまりいいものが思いつかない

  • Array#pop
  • RegExp#exec (nullを返す貴重なもの)

from js-primer.

azu avatar azu commented on August 19, 2024

for...of についてはやっぱりiterableという概念があり、ArrayやStringなど多くのオブジェクトがIterableであるという話だけにするのが良さそう。

Learning JavaScript, 3rd Edition - O'Reilly Mediaではその程度の話にしていて、残りはIterableの章でって感じになっていた

from js-primer.

azu avatar azu commented on August 19, 2024

ユースケース

  • for
    • 数の合計
    • 配列の列挙
  • while
    • カウントダウン
    • 副作用 - 正規表現のexec?/配列のpop()?
  • do-while
    • コンソール出力
  • breakとcontinue
    • break; 多次元配列からの探索
    • continue; ループにおける早期return
  • while - 無限ループ
  • for...in
  • for...of
    • iterateの概念解説

from js-primer.

azu avatar azu commented on August 19, 2024

これ先に配列とオブジェクトのデータへのアクセス方法をちゃんと書いた方が良さそう。
(ユースケースにかなり触れにくい)
オブジェクトは データ型とリテラルで書いているので、配列も同じ場所に array[index] でアクセスできるというのを書いたほうがよさそう。

from js-primer.

azu avatar azu commented on August 19, 2024

MDNでは次のような抽象構文使ってた。

for ([initialExpression]; [condition]; [incrementExpression])
  statement

となっていて incrementExpression が何でこの名前なんだと思ってたら

13.7.4.8Runtime Semantics: ForBodyEvaluation( test, increment, stmt, perIterationBindings, labelSet )#
-- http://www.ecma-international.org/ecma-262/7.0/#sec-forbodyevaluation

仕様でこの3番目のExpressionを increment としてるからかー。
日本語に当てはめるのむずそう。増分式とか謎すぎる言葉になる。

from js-primer.

azu avatar azu commented on August 19, 2024

IonMonkey: Evil on your behalf | JavaScript

for ofがfor文より2.5x遅い程度の差で動くのか。
for ofとかforEachとかはどれだけ上手く最適なパスに載っても、for文と同等になる感じ。

function norm1(vec) {
    var sum = 0;
    vec.forEach((x) => { sum += x; });
    return sum;
}

はScalar Replacementによってfor文と同等に展開されるのか。
forEachとかのほうがスコープによる安全性があるので、for文よりArrayメソッドのほうがいいな
(このケースだとreduce使いたいけど)

from js-primer.

azu avatar azu commented on August 19, 2024

for...inの順序 不定なのかな。でも大体決まったりしてるとかそういう感じなのかな

The mechanics and order of enumerating the properties is not specified but must conform to the rules specified below.

from js-primer.

januswel avatar januswel commented on August 19, 2024

for...inの順序 不定なのかな。でも大体決まったりしてるとかそういう感じなのかな

The mechanics and order of enumerating the properties is not specified but must conform to the rules specified below.

https://tc39.github.io/ecma262/#sec-enumerate-object-properties

below の部分で述べられているのは次のことで、列挙される順序については何も書いていないですね。他の言語と同じく不定なのでは。

  • iterator の throwreturn は null になる
  • 返されるキーに Symbol は含まれない
  • 列挙中にプロパティを削除してよい
  • 列挙中に新しいプロパティを追加しても列挙されることが保証されない
  • ひとつのプロパティは一回だけ列挙される
  • prototype チェインをさかのぼる

from js-primer.

azu avatar azu commented on August 19, 2024

長い長い反復処理はひとまずおわり

ラベルについては今現在は必要ないもの(必要な人は自分で探せるはず)なので書いてない(コラムにしかならない感じ)
reduceのやつは議論の余地があるというか邪魔だったら消して問題ない気がするレベル。。
for...in文は異常なくらい書いてて難しさを感じる…

from js-primer.

azu avatar azu commented on August 19, 2024

tc39/proposals@d26dd0a leobalter/object-enumerables: Object.enumerableKeys / Object.enumerableValues / Object.enumerableEntriesはrejectされた

from js-primer.

azu avatar azu commented on August 19, 2024

for vs forEach The for Loop vs. forEach in JavaScript - The JS Guy - David Tang

from js-primer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.