Skip to content

Instantly share code, notes, and snippets.

@dansajin
Last active April 5, 2016 13:20
Show Gist options
  • Save dansajin/75f9f55ee26f1a4568cd773f3c33bad5 to your computer and use it in GitHub Desktop.
Save dansajin/75f9f55ee26f1a4568cd773f3c33bad5 to your computer and use it in GitHub Desktop.
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