Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS restore default/global functions

This is a hypothetical question, it really doesn't have a practical use, but...

Let's say you were to do:

document.open = null;

How would one restore document.open to its original functionality, is this possible (without user-made temporary storage)? Is document.open stored in another location under a less known name? Thankyou! :)

like image 307
Georges Oates Larsen Avatar asked Jul 09 '12 22:07

Georges Oates Larsen


2 Answers

Overwriting document.open creates a variable/function named open directly on the document object. However, the original function was not on the object itself but its prototype - so you can indeed restore it.

The open function is from HTMLDocument.prototype so you can access it using HTMLDocument.prototype.open.

To call it directly, use .call() to specify the object to use it on:

HTMLDocument.prototype.open.call(document, ...);

You can also restore document.open it by simply assigning it:

document.open = HTMLDocument.prototype.open;

However, remember that HTMLDocument and thus document are host objects and it's usually a good idea not to mess with them - especially in IE things are likely to go haywire if you do so.

like image 157
ThiefMaster Avatar answered Sep 30 '22 20:09

ThiefMaster


delete document.open;

It's not intuitive, but using the delete keyword on a customized function will restore the original function, at least as long as the prototype hasn't been overwritten.

Example:

> console.log
function log() { [native code] }

> console.log = function() { }
function () { }

> console.log("Hello world");
undefined

> delete console.log;
true

> console.log("Hello world");
Hello world

Works the same way with document.open and other built-in functions.

like image 44
Patrick Mylund Nielsen Avatar answered Sep 30 '22 22:09

Patrick Mylund Nielsen