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

Link to Random WordPress Post

  1. $p = get_posts(array('orderby' => 'rand', 'showposts' => 1));
  2. echo '<a href="'.get_permalink($p[0]->ID).'">Random</a>';

I have been wanting to add a link to the header of this site that will randomly open a snippet zone post… Finally got around to it the other day – figured I’d post the lazy php code for it 😀

// php // wordpress

Super Simple Seeded Random

  1. let _seed = 1234567;
  2.  
  3. // Deterministic pseudo-random float in the interval [ 0, 1 ]
  4. function seededRandom( s ) {
  5.   if ( s !== undefined ) _seed = s % 2147483647;
  6.  
  7.   // Park-Miller algorithm
  8.   _seed = _seed * 16807 % 2147483647;
  9.   return ( _seed - 1 ) / 2147483646;
  10. }

Straight from the THREE.js source code – a fun and simple seeded random. The best libraries are always filled with gems like this…

If I were going to use this I would do a couple things out of pure preference:

  1. let seed = 1234567;
  2.  
  3. const setSeed = newSeed => seed = newSeed
  4. function seededRand() {
  5.   // Park-Miller algorithm
  6.   seed *= 16807 % 0x7fffffff;
  7.   return (seed - 1) / 0x7fffffff;
  8. }
  9.  
  10. // try it out:
  11.  
  12. console.log('one', seededRand());
  13. console.log('two', seededRand());
  14. console.log('three', seededRand());
  15.  
  16. seed = 9999
  17. console.log('one new seed', seededRand());
  18. console.log('one new seed', seededRand());
  19.  
  20. seed = 1234567;
  21. console.log('one old seed', seededRand());

If you’re wondering about what this is doing… read more about it here.

Bring Node to Top

  1. myEl.parentNode.appendChild(myEl);

This will make an element the top most of all of its siblings…

  1. document.body.innerHTML += `
  2. <div>
  3.   <button>one</button>
  4.   <button>two</button>
  5.   <button>three</button>
  6.   <button>four</button>
  7. </div>
  8. <style>
  9.   button { display: block; cursor: pointer; }
  10. </style>
  11. `;
  12. document.addEventListener('click', e => {
  13.   if (e.target.tagName === 'BUTTON') {
  14.     e.target.parentNode.appendChild(e.target);
  15.   }
  16. });

Clicking on any button will bring it to the top most position within its parent.

// dom // javascript // tricks // ui

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

valueOf for Shorter Function Calls

  1. r={valueOf:_=>Math.random()}
  2. console.log(+r)
  3. console.log(+r)
  4. console.log(+r)

Use valueOf to shorten function calls. I learned this trick over at stackexchange codegolf here from user cyoce.

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