美文网首页我爱编程
typescript中 循环for of与其他循环的区别

typescript中 循环for of与其他循环的区别

作者: Allen6879 | 来源:发表于2018-01-29 10:58 被阅读0次

    1.JavaScript中的forEach用法。它循环的结果是数组的元素!可以有最多三个参数,但是不能break中断循环,如下:

    var arr = [1,2,3,4];

    arr.forEach(function(value,index,array){

        array[index] == value;    //结果为true

        });

    //index为下标

    //array为数组本身

    2:ts中的 for of循环,用法类似于forEach,但是能中断循环

    for (x of arr){

      if(x==2){

        break;

    }

    console.log(arr[x]);

    }

    3:for in 循环;用法和for  of 一样。但是它能遍历属性,如下:

    arr.name = "这是个数组";

    for (x in arr){

          console.log(arr[x])

    }

    //结果 1 2 3 4  这是个数组

    相关文章

      网友评论

        本文标题:typescript中 循环for of与其他循环的区别

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