Skip to content

Instantly share code, notes, and snippets.

@dansajin

dansajin/overrideOnce.js

Last active Apr 5, 2016
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