zoukankan      html  css  js  c++  java
  • 十一. for of

    const fruits = ['Apple','Banana','Orange','Mango'];
    

      

    es5: 可读性差

    for(let i=0; i < fruits.length; i ++){
       console.log(fruits[i]);
    }

    // Apple
    // Banana
    // Orange
    // Mango

      

    es6箭头函数:

    一. 注释: 缺点 不能终止或跳过循环 以下按钮会报错
    fruits.forEach(fruit =>{
     console.log(fruit);
    })
    fruits.forEach(fruit =>{
     if(fruit === 'Orange'){
      break;
     } console.log(fruit); })
    fruits.forEach(fruit =>{
     if(fruit === 'Orange'){
      continue;
     } console.log(fruit); })

    二. 注释: 会把原型上的属性跟内容也返回回来
    for (let index in fruits){
      console.log(fruits[index]);
    }
    
    Array.prototype.first = function(){
      return this[0];
    }
    const fruits = ['Apple', 'Banana', 'Orange', 'Mango'];
    fruits.describe = 'My favorite fruits';
    
    for (let index in fruits){
      console.log(fruits[index]);
    }
    
    // Apple
    // Banana
    // Orange
    // Mango
    // My favorite fruits
      function(){
        return this[0];
      }
    

      

    三:终极版,解决了上面循环的缺陷 不会打印出原型上一些属性,支持跳出循环
    for(let fruit of fruits){
      console.log(fruit);
    }
    // Apple
    // Banana
    // Orange
    // Mango
    
    for(let fruit of fruits){
      if(fruit === 'Orange'){
        break;  //终止
      }
      console.log(fruit);  // Apple Banana
    }
    
    for(let fruit of fruits){
      if(fruit === 'Orange'){
        continue;  //跳出循环
      }
      console.log(fruit);  // Apple Banana Mango
    }
    

      

  • 相关阅读:
    http://www.jdon.com/jivejdon/thread/37340
    我的英语死在类似的问题上
    Linux之read命令使用
    SIP注册呼叫流程简介
    sh里的变量 $0 $1 $$ $#
    LTE 逻辑分层和接口协议
    LTE语音业务VOLTE
    shell编程——if语句 if z n f eq ne lt
    高通QXDM抓modem log
    LTE与VOLTE基础知识
  • 原文地址:https://www.cnblogs.com/wangRong-smile/p/11927206.html
Copyright © 2011-2022 走看看