SET !

  1. const set1 = new Set([2,1,3]);
  2. const set2 = new Set([4,5,6,3]);

  3. let union = [...[...set1],...[...set2]];

  4. let t = new Set(union.sort( (a,b) => a-b));
trie un set ! 


début de MVC

 Le contrôleur

  1. class Controleur { constructor(view) { this.view = view; this.name = "control" this.bind(); } bind() { console.log(this.name); this.view.bindAdd(()=>this.handlerAdd()); } handlerAdd() { let data = new Date(); this.view.handlerAdd(data); } }
  2. c = new Controleur(new View());

 La view

  1. class View { constructor() { this.name = "view" this.bAdd = document.querySelector(".add"); this.l = document.querySelector("ol"); } handlerAdd(data) { console.dir(this.name); this.l.insertAdjacentHTML("afterbegin", `<li> ${data}</li>`); } bindAdd(handler) { this.bAdd.addEventListener("click", () => { console.log(this.name); handler(); }) } }

 HTML

<body>
    <button class="add" name="buttonAdd" type="submit"> + </button>
    <ol></ol>
    <script src="view.js"></script>
    <script src="controleur.js"></script>
</body>

filter : thisarg

 

  1. function majeur( {born, sex} ) {

  2.   return (born < this.born  && sex == this.sex) }


  3. let f = {

  4.   sex : "f",

  5.   born : 2002,

  6. }


  7. let h = {

  8.   sex : "h",

  9.   born : 2000,

  10. }


  11. let hommesMajeur = tabPers.filter(majeur,h),

  12.      femmesMajeur = tabPers.filter(majeur,f);




code


2020

Switch case en action

voici le test pour rechercher dans un string les caractère < ou >
  1.     switch (string.charCodeAt(index)) {
  2.        case 60: // <
  3.         escape = '&lt;';
  4.         break;
  5.       case 62: // >
  6.         escape = '&gt;';
  7.         break;
  8.       default:
  9.         continue;
  10.     }
code

module

ES6

  1. // List of the characters to escape 
  2. const chars = {
  3.     ">": "&gt;",
  4.     "<": "&lt;",
  5. };

  6. //  RegExp from the `chars` object
  7. const re = new RegExp(Object.keys(chars).join("|"), "g");

  8. // Return the escaped string
  9. const escapeHtml = (str = "") => String(str).replace(re, match => chars[match]);

  10. console.dir( escapeHtml('<div>'));


Filtre


  1. const filter = 

  2.   (fx, array) => array.reduce(

  3.     (acc, item) => fx(item) ? acc.concat(item) : acc, 

  4.   []);

  5. const greaterThan4 = (x) => x >= 4;

  6. const data   = [0, 1, 2, 3, 4, 5];

  7. let result = filter(greaterThan4, data);


code