prompt - non-recursively replace built-in javascript functions -
i writing bookmarklets here , have questions related built-in javascript functions.
let's want replace built-in prompt function (not in bookmarklet). seems easy enough, there way call builtin prompt function within replacement?
prompt = function(message){ var tmp = prompt(message); hook(tmp); return tmp; }
i couldn't scoping work out right; example yields infinite recursion.
also there way restore default behavior of builtin javascript function has been replaced (without hanging on reference).
(function () { var old_prompt = prompt; prompt = function (msg) { var tmp = old_prompt(msg); hook(tmp); return tmp; }; prompt.restore = function () { prompt = old_prompt; } // analogous other functions want replace })();
wrapping in (self-executing) function ensures old_prompt
doesn't leak outside. need expose something though. chose provide function doing restoring, convenience , perhaps, 1 say, future-proofing , encapsulation. long higher order functions refrain fiddling else's scope...
also, no, it's (i'd assume) not possible restore previous value of variable without reference (the old value), if value happened built-in. if possible, it'd pretty obscure trick - way works, let's stick it.
(credit func.restore
goes martijn)
Comments
Post a Comment