special function

debounce data

Operation DOM:

let temp = document.getElementsByTagName("a"); for (let i of temp) { console.log(i.innerHTML); }

Random array

for (let i = temp.length - 1; i >= 0; i--) {

let j = Math.floor(Math.random() * (i + 1));

[temp[i], temp[j]] = [temp[j], temp[i]];

}

7 ways to flip two varibles

a = "hello", b = "bye";

  1. var c = a; a = b; b = c;

  2. a = a + b; b = a - b; a = a - b; -------- a = a - b; b = a + b; a = b - a;

  3. a ^= b; b ^= a; a ^= b; a = (b^=a^=b)^a;

  4. a = {a:b,b:a}; b = a.b; a = a.a;

  5. a = [a,b]; b = a[0]; a = a[1];

  6. a = [b,b=a][0]; -------

    b = [a, a = b][0];
    
    a = b + (b = a, "");
  7. [a,b] = [b,a];

generator

Generator function

function* generator(i) { yield i; yield i + 10; }

GeneratorFunction

Object.getPrototypeOf(function*(){}).constructor

function* expression

const foo = function*() { yield 'a'; yield 'b'; yield 'c'; };

Generator.prototype.next()

Returns a value yielded by the yield expression.

Generator.prototype.return()

Returns the given value and finishes the generator.

Generator.prototype.throw()

Throws an error to a generator (also finishes the generator, unless caught from within that generator).

function* infinite() {
    let index = 0;

    while (true) {
        yield index++;
    }
}

const generator = infinite(); // "Generator { }"

console.log(generator.next().value); // 0
console.log(generator.next().value); // 1

Last updated

Was this helpful?