Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am uri-chandler on github.
  • I am uri_chandler (https://keybase.io/uri_chandler) on keybase.
  • I have a public key ASCbMMnVpA2UroLrW9ICpfPjfH7Szmi_7rmgGFxSslTNPAo

To claim this, I am signing this object:

Keybase proof

I hereby claim:

To claim this, I am signing this object:

@uri-chandler
uri-chandler / wait-sync-example.js
Created October 24, 2016 20:53
[blog] wait sync example 1
// using wait-sync, you can use both async *and* sync code in the same execution block
// without worring about blocking the thread (so node's event-loop keeps running, I/O still works as expected etc..)
var waitSync = require('wait-sync');
// the easy part - just log "1" to the console
console.log(1);
// next, setup some async operation that will log "2" sometime in the future - say, 3 seconds from now
setTimeout(() => console.log(2), 3000);
@uri-chandler
uri-chandler / apply-apply.js
Last active March 15, 2016 07:57
[Case study: Applying advanced javascript] Code 2
// In older IE, this function doesn't inherit from Function.prototype
// so there's no .apply() we can use.
// This will throw an error
document.getElementById.apply(document, ['myDiv']) // Error!
// The solution:
(Function.prototype.apply).apply(document.getElementById, [document, ['myDiv']]); // Smooth!
@uri-chandler
uri-chandler / apply-example.js
Created March 14, 2016 21:11
[Case study: Applying advanced javascript] Code 1
function greet(name){
console.log('hello ' + name);
}
// We usually invoke a function by writing it's name followed by parenthesis
greet('John'); // hello John
// Another way - using .apply()
@uri-chandler
uri-chandler / using-events.js
Created March 2, 2016 22:07
[Loose Coupling Is A Lie] Code Listing 3
/**
* Example of loose coupling using events
*/
function work(){
this.emit('working');
}
var shovel = {
@uri-chandler
uri-chandler / loose-coupling.js
Created March 2, 2016 19:01
[Loose Coupling Is A Lie] Code Listing 2
/**
* Example of loose coupling between work() and shovel.use()
*/
function work(tool){
tool.use();
}
var shovel = {
@uri-chandler
uri-chandler / tight-coupling.js
Last active March 2, 2016 19:01
[Loose Coupling Is A Lie] Code Listing 1
/**
* Example of tight coupling between work() and shovel.use()
*/
function work(){
shovel.use();
}
var shovel = {
@uri-chandler
uri-chandler / ClearAndReload.js
Last active August 29, 2015 14:15
Clear cookies and storage, then reloads the page
(function(){
// First, delete all cookies
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
var eqPos = cookie.indexOf("=");
var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
@uri-chandler
uri-chandler / arrayify.js
Created October 30, 2014 21:47
Add array-like accessors to any object
// Adds array-like accessors to 'obj'
function arrayify(obj){
// Init
var newObj = {},
idx = 0;
// Add array-like keys to all self-properties
for (var key in obj){ if (obj.hasOwnProperty(key)){