Skip to content

Instantly share code, notes, and snippets.

@specktator
Last active October 15, 2015 22:12
Show Gist options
  • Save specktator/2ef980a4964faaee366d to your computer and use it in GitHub Desktop.
Save specktator/2ef980a4964faaee366d to your computer and use it in GitHub Desktop.
// see it running at http://jsbin.com/qofozulihu/2/edit?js,console,output
// This gist is just an example idea of running functions and having fallbacks for them
fallbacks={
one:function(){
console.log('one');
return false;
},
two:function(){
console.log('two');
return true;
},
three:function(){
console.log('three');
return false;
}
};
function fallbacking(fallbacksObj){
prevFallbackResult = false; //pre-declaring variable
for (var index in fallbacksObj ){
if(!prevFallbackResult){ // if method returns false then the loop continues to run/call the next methods until there's no other method to run or true is returned.
prevFallbackResult = fallbacks[index]();
}
}
}
fallbacking(fallbacks); //run it
// register another method
fallbacks.anotherFallback = function(){
console.log('another one');
return false;
};
// next run
fallbacking(fallbacks);
// change the second fallback to return something
fallbacks.two = function(){
console.log('stop there! I found it!');
return true;
};
fallbacking(fallbacks); // run again
//do some transportation patching
fallbacks.last = fallbacks.two;
fallbacks.two = function(){
console.log("Now I'm gonna fail!");
return false;
};
fallbacking(fallbacks); // run again but patched
//do some monkey patching
oldThree = fallbacks.three;
fallbacks.three = function(){
console.log('Before: I am a code Monkey!');
oldThree();
console.log('After: I am a code Monkey!');
};
fallbacking(fallbacks);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment