)
}
}
)
(
}
{
)
)
(
)
(
(
{
}
)
(
)
}
)
)
{
(
(
)
)
}
)
(
}

Martin Kleppe’s Golfed Blobs Quine

  1. <pre id=p style=background:#000><svg onload='setInterval(f=n=>
  2. {for(t++,o=i=1;i++<476;o+=i%30?([(f+"")[i%195],"o"][c=0|(h=v=>
  3. (M=Math).hypot(i/30-8+3*M.sin(t/8/v),i%30/2-7+4*M.cos(t/9/v)))
  4. (7)*h(9)*h(6)/52]||".").fontcolor(c?c>2:n):"\n");p.innerHTML=o},t=1)'>

Great golfed snippet from Martin Kleppe – can’t wait to fork it… 😀

This was updated a bit later to be even smaller:

  1. <body onload='setInterval(f=n=>{for(t++,o=i=1;i++<476;o+=i%30?([(f+f)[i],"o"][c=0|(h=v=>(M=Math).hypot(i/30-7+3*M.sin(t/8/v),i%30/2-7+4*M.cos(t/9/v)))(7)*h(9)*h(6)/52]||".").fontcolor(c?c>2:n):"\n");p.innerHTML=o},t=1)'bgcolor=X><pre id=p>
// dom // golfed // graphics // hacks // html // humor // javascript // math // strings // tricks

Bind All Methods to a Class instance JavaScript

  1. function bindAll(target) {
  2.   const keys = Object.getOwnPropertyNames(target.constructor.prototype);
  3.   for (let i = 0; i < keys.length; i++) {
  4.     const key = keys[i];
  5.     const propOrMethod = target[key];
  6.     if (typeof propOrMethod === 'function') {
  7.       target[key] = target[key].bind(target);
  8.     }
  9.   }
  10. }
  11.  
  12. class Test {
  13.   constructor() {
  14.     bindAll(this);
  15.     document.addEventListener('click', this.onClick);
  16.   }
  17.   onClick(e) {
  18.     console.log('click', this);
  19.   }
  20.   otherMethod(e) {
  21.     console.log('test', this);
  22.   }
  23. }
  24.  
  25. const test = new Test();
  26.  
  27. setInterval(test.otherMethod, 1000);

This is useful when you know you need many methods of a given class to be bound to the classes instance. Another way to do this is to selectively use instance properties:

  1. class Test {
  2.   constructor() {
  3.     document.addEventListener('click', this.onClick);
  4.   }
  5.   onClick = e => {
  6.     console.log('click', this);
  7.   }
  8.   otherMethod e => {
  9.     console.log('test', this);
  10.   }
  11. }

Avoiding classes is another way to not have to deal with this issue 😉

// humor // javascript // oop

Obfuscated Pseudo-typed Globals

  1. number:age = 33;
  2. number:height = 6;
  3. string:name = 'joe';
  4. object:person = { name, age, height };
  5. console.log(person);

WARNING: do not use this

This one is bad enough that I feel the need to put a warning. This is some weird obfuscation… I’ve put labels before global variable definitions so they look like type declarations.

It’s funny, monaco says lots of stuff about this one if you hit the Try it out… button you can see…

Guess the semi-golf

  1. a=3217+'';a=[a];console.log(a[0][2])

Try and guess what would be logged here…

Array Range from 1

  1. Object.assign(Number.prototype, {
  2.   *[Symbol.iterator]() {
  3.     for (let i = this; i--;) 
  4.       yield this - i;
  5.   }
  6. });
  7.  
  8. console.log([...3]);

I saw this on twitter a little while back. Tweet was from James Padolsey. A fun trick, the thread is pretty entertaining.

snippet.zone ~ 2021-24 /// {s/z}