for-in statement

后端 未结 4 1199
花落未央
花落未央 2020-11-30 07:07

TypeScript docs say nothing about loop like for or for-in. From playing with the language it seems that only any or string

4条回答
  •  無奈伤痛
    2020-11-30 07:50

    edit 2018: This is outdated, js and typescript now have for..of loops.
    http://www.typescriptlang.org/docs/handbook/iterators-and-generators.html


    The book "TypeScript Revealed" says

    "You can iterate through the items in an array by using either for or for..in loops as demonstrated here:

    // standard for loop
    for (var i = 0; i < actors.length; i++)
    {
      console.log(actors[i]);
    }
    
    // for..in loop
    for (var actor in actors)
    {
      console.log(actor);
    }
    

    "

    Turns out, the second loop does not pass the actors in the loop. So would say this is plain wrong. Sadly it is as above, loops are untouched by typescript.

    map and forEach often help me and are due to typescripts enhancements on function definitions more approachable, lke at the very moment:

    this.notes = arr.map(state => new Note(state));
    

    My wish list to TypeScript;

    1. Generic collections
    2. Iterators (IEnumerable, IEnumerator interfaces would be best)

提交回复
热议问题