Skip to content

Instantly share code, notes, and snippets.

@amacdougall
Created January 15, 2013 22:48
Show Gist options
  • Save amacdougall/4542912 to your computer and use it in GitHub Desktop.
Save amacdougall/4542912 to your computer and use it in GitHub Desktop.
// replacing a function with a one-time suppression using _.wrap
var module = {
execute: function(target) {
doSomeBigAction(target);
}
};
// it might be more clear if I assign this intermediate variable
var skipOnce = _(module.execute).wrap(function(original) {
console.log("Sorry, doing nothing this time.");
module.execute = original;
});
module.execute = skipOnce; // replace existing execute method
module.execute(command); // nothing happens
module.execute(command); // does the action
// altering a function using _.wrap
module.execute = _(module.execute).wrap(function(original, target) {
original(target);
additionalActions(target);
/* Does not change itself back; though you can see how it would
* be possible to change itself back conditionally or something.
*/
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment