Array.from + itérateur

let range = {
  from: "A",
  to: "W"
};

// 1. call to for..of initially calls this
range[Symbol.iterator] = function() {

  // 2. ...it returns the iterator:
  return {
    current: this.from,
    last: this.to,

    // 3. next() is called on each iteration by the for..of loop
    next() {
      // 4. it should return the value as an object {done:.., value :...}
      if (this.current <= this.last) {
let char = this.current;
this.current = String.fromCharCode(this.current.charCodeAt(0)+1);
        return { done: false, value: char };
      } else {
        return { done: true };
      }

    }
  };
};

for (let num of range) {
  console.log(num);
}

let t = Array.from(range,c=>c.toLowerCase());
console.log(t);

code


JS Bin on jsbin.com

Array.from : magic

t = Array.from(Fib)
console.log(t);

[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]

fichier code

code

En action : 

let Fib = {
  [Symbol.iterator]() {
    let n1 = 1,
      n2 = 1;
    return {
      [Symbol.iterator]() {
        return this;
      },
      next() {

          let current = n2;
          n2 = n1;
          n1 = n1 + current;
          if (current <= 1000) { //sinon la boucle est infinie
            return {
              value: current,
              done: false
            };
          } else {
            return {
              done: true
            };
          }
          return {
            value: current,
            done: false
          };
        },
        return () {
          console.log(
            `stop`
          );
          return {
            done: true
          };
        }
    };
  }
};

t = Array.from(Fib)
console.log(t);

itérator :

Voici deux écritures de Fibonacci :

const Fibonacci = {
    n1: 1,
    n2: 1,
    [Symbol.iterator]() {
      return this;
    },
    next() {
      let current = this.n2;
      this.n2 = this.n1;
      this.n1 = this.n1 + current;
      return {
        value: current,
        done: false
      };
    },
    return (v) {
      return {
        value: v,
        done: true
      };
    }
  };

let Fib = {
  [Symbol.iterator]() {
    let n1 = 1,
      n2 = 1;
    return {
      [Symbol.iterator]() {
        return this;
      },
      next() {
          let current = n2;
          n2 = n1;
          n1 = n1 + current;
          return {
            value: current,
            done: false
          };
        },
        return () {
          console.log(
            `stop`
          );
          return {
            value: v,
            done: true
          };
        }
    };
  }
};
for (var v of Fib) {
  console.log(v);
  if (v > 50) break;
}

itérateur

let range = {
  from: 65,
  to: 90,
  [Symbol.iterator]() {
    return this;
  },
  next() {
    if (this.from <= this.to) {
      return {
        done: false,
        value: String.fromCharCode(this.from++)
      };
    } else {
      return {
        done: true
      };
    }
  },
};



// A-W
for (let num of range) {
  console.log(num); 
}

module

http://dupontcours.free.fr/ES6/module/

la décompostion

décomposition et renomage :

const cours = [
  {
    langue: "fr",
    niveau: {
      lu: 2,
      ecrit: 5,
      parle: 5
    },
    test: 35
  },
  {
    langue: "eng",
    niveau: {
      lu: 4,
      ecrit: 4,
      parle: 4
    },
    test: 100
  }
]

// sans renomage
for (let {langue, niveau: { ecrit} } of cours) {
  console.log(`langue : ${langue}
\t\tniveau de l'écrit = ${ecrit}`)

}

// on peut redéfinir les variables
for (let {langue: l, niveau: { ecrit: e } } of cours) {
  console.log(`langue : ${l}
\t\tniveau de l'écrit = ${e}`)
}

>
  • "langue : fr
        niveau de l'écrit = 5"
  • "langue : eng
        niveau de l'écrit = 4"