Last active
April 5, 2016 13:20
-
-
Save dansajin/75f9f55ee26f1a4568cd773f3c33bad5 to your computer and use it in GitHub Desktop.
Javascript override method only once wrapper function
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
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