Created
January 15, 2013 22:48
-
-
Save amacdougall/4542912 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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