JS restore standard / global functions

This is a hypothetical question, it really has no practical application, but ...

Let's say you had to do:

document.open = null; 

How to restore document.open document to its original functionality, is it possible (without user temporary storage)? Is document.open stored elsewhere under a lesser known name? Thanks!:)

+6
source share
3 answers

Overwriting document.open creates a variable / function called open directly on the document object. However, the original function was not on the object itself, but on its prototype, so you can really 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:

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

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

 document.open = HTMLDocument.prototype.open; 

However, remember that HTMLDocument and therefore document are HTMLDocument objects, and it's usually a good idea not to bother with them - especially in IE, things are likely to be elusive if you do.

+9
source
 delete document.open; 

This is not intuitive, but using the delete keyword in a custom function will restore the original function, at least until the prototype is 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 

It works similarly with document.open and other built-in functions.

+4
source
 var temp = document.open; document.open = null; 

and then restore the original function with

 document.open = temp; 
+1
source

Source: https://habr.com/ru/post/920087/


All Articles