美文网首页
Array 遍历

Array 遍历

作者: 晨曦Bai | 来源:发表于2020-05-08 13:52 被阅读0次

    https://www.typescriptlang.org/docs/handbook/iterators-and-generators.html

    可以迭代的类型: Array , Map , Set , String,Int32Array, Uint32Array,etc.

    1. for ... of vs for ... in

    for (let i in list) {} // i 是 index / key
    for (let i of list) {} // i 是 value

    let list = [4, 5, 6];
    
    for (let i in list) {
        console.log(i); // "0", "1", "2",
    }
    
    for (let i of list) {
        console.log(i); // "4", "5", "6"
    }
    
    • Code Generation
    1. Targeting ES5 and ES3

    当编译目标语言为 ES5 and ES3
    for ... of 转换成标准的 C
    only allowed on values of Array type

    let numbers = [1,2,3];
    for (let num of numbers) {
    console.log(num);
    }
    
    // generated as
    
    var numbers = [1,2,3];
    for(var _i = 0; _i < numbers.length; _i++){
    console.log(num);
    }
    
    1. Targeting ECMAScript 2015 and higher
      the compiler will generate for ... of

    相关文章

      网友评论

          本文标题:Array 遍历

          本文链接:https://www.haomeiwen.com/subject/ggexnhtx.html