Skip to content

Instantly share code, notes, and snippets.

@dansajin
Last active April 5, 2016 13:20
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Javascript override method only once wrapper function
function overrideOnce(object, method, callback){
// override method with original as argument
object[method] = (function(original){
return function(){
// store whatever callback does
var result = callback.apply(this, arguments);
// restore method
object[method] = original;
return result;
}
}(object[method]));
}
// usage
overrideOnce(document, 'write', function(html){
$('body').append(html);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment