cms's notebooks

  • weekday - /cms/weekday
    Last edited 4 years ago
    function weekday(day, locale = 'es') { if (day > 7 || day < 1) return "Wrong, please enter a number between 1 and 7" return new Date(1970, 1, day).toLocaleString(locale, {weekday: 'long'}) } console.log(weekday(1)) // "domingo" console.log(weekday(2)) // "lunes" //... console.log(weekday(7)) // "sábado" // Otros idiomas: console.log(weekday(1, 'en')) // "Sunday" console.log(weekday(1, 'it')) // "domenica" console.log(weekday(1, 'ja')) // "日曜日"
  • Proxy in prototype chain - /cms/proxy-in-prototype-chain
    Last edited 4 years ago
    function Person(first, last, age) { this.first = first; this.last = last; this.age = age; } Person.prototype.greeting = function () { return `Hello my name is ${this.first} and I am ${this.age} years old`; }; let validator = { set: function(target, key, value, receiver) { console.log(`The property ${key} has been updated with ${value}`); target[key] = value; console.log(receiver.first); return true; } }; Person.prototype = new Proxy(Person.prototype, validator); let george = new Person('George', 'Clooney', 55) Person.prototype.farewell = function () { return `Hello my name is ${this.first} and I will see you later`; }; george.foo = 'bar' Person.prototype
  • async iterator example - /cms/async-iterator-example
    Last edited 4 years ago
    (async () => { const iterator = asyncRangeGenerator(1, 3); console.log(iterator.next()); // Promise console.log(await iterator.next()); // Object {value: 2, done: false} console.log(await iterator.next()); // Object {value: 3, done: false} console.log(await iterator.next()); // Object {value: undefined, done: true} const iterator2 = asyncRangeGenerator(1, 5); for await (const num of iterator2) { console.log(num); } })(); function delay(timeout, value) { return new Promise(resolve => setTimeout(() => resolve(value), timeout)); } async function* asyncRangeGenerator(start, end) { while (start <= end) { yield delay(1000, start++); } }
  • throttle implementation - /cms/throttle
    Last edited 5 years ago
    //let _throttle = require("lodash/throttle") let lastTime; function fn(...args) { console.log(args, Date.now()); if (!lastTime) lastTime = Date.now(); else console.log("diff:", Date.now() - lastTime); } var fnThrottled = throttle(fn, 200); function throttle(fn, wait) { let lastCall, timer; return function() { let now = Date.now(); if (lastCall && now < lastCall + wait) { clearTimeout(timer); timer = setTimeout(() => { lastCall = now; fn.apply(this, arguments); }, wait); } else { lastCall = now; fn.apply(this, arguments); } }; } fnThrottled(1, 2, 3); fnThrottled(1, 2, 3); fnThrottled(1, 2, 3); fnThrottled(1, 2, 3); fnThrottled("a", "b", "c");
  • Diferencias entre metodos concisos y funciones normales - /cms/diff-metodo-conciso
    Last edited 5 years ago
    const obj1 = { // Sintaxis de metodo conciso (ES6) metodo() { // diferencia1: se puede accesar a super console.log(super.toString === Object.prototype.toString) } }; const obj2 = { metodo: function () { } }; // diferencia 2: el sintaxis conciso crea una funcion con nombre por defecto // equivalente a ( { metodo: function metodo() {} } ) console.log(obj1.metodo.name); // "metodo" console.log(obj2.metodo.name); // undefined // diferencia 3: las funciones que resultan de la declaracion de un metodo conciso // no son "construibles" ( no se pueden usar con el operador `new` ) try { new obj1.metodo() } catch (error) { console.log(error); // TypeError: obj1.metodo is not a constructor } console.log(new obj2.metodo()) // no hay error