object.prototype question

S

steve dp

Lets say we run: window.alert = function() { };

Is there anyway to 'restore' the original alert() method or is it gone
forever?

I know you can do window.alert = Window.prototype.alert, but lets say
you also set Window.prototype.alert = function() { } or lets say we're
in Opera, which doesnt have a Window "class".
 
V

VK

steve said:
Lets say we run: window.alert = function() { };

Is there anyway to 'restore' the original alert() method or is it gone
forever?

Are you trying to keep a possibility to restore alert() or are you
trying to eliminate such possibility?

In any case by doing window.alert = function() { }; you are not
overriding the native code, you merely replace the reference. So it's
enough to:

window.nativeAlert = window.alert;
window.alert = function() { };
....
window.alert = window.nativeAlert;

If you want to shadow the native method, when my strike back hack :)
would be to create new hidden iframe - which is window by its nature -
and use its fresh 'non-spooffed' references.
 
R

RobG

steve said:
Lets say we run: window.alert = function() { };

Is there anyway to 'restore' the original alert() method or is it gone
forever?

No, it hasn't 'gone' anywhere. You've just overridden the reference to it.
I guess a cynic might say that if you remove all references it'll be
swallowed by garbage collection :) but I doubt that can happen with
in-built host objects.

I know you can do window.alert = Window.prototype.alert, but lets say
you also set Window.prototype.alert = function() { } or lets say we're
in Opera, which doesnt have a Window "class".

Store a reference to it, then restore it when you're finished. Play with this:

<p>Test window.alert</p>
<input type="button" value="test alert"
onclick="alert('hey, it worked');">

<p>Store a reference to window.alert</p>
<input type="button" value="store reference"
onclick="window.keptAlertRef = window.alert;">

<p>Over-ride window.alert</p>
<input type="button" value="over-ride alert"
onclick="window.alert = null;">

<p>Restore window.alert from stored reference</p>
<input type="button" value="restore reference"
onclick="window.alert = window.keptAlertRef;">
 
T

Thomas 'PointedEars' Lahn

RobG said:
No, it hasn't 'gone' anywhere. You've just overridden the reference to
it. I guess a cynic might say that if you remove all references it'll be
swallowed by garbage collection :) but I doubt that can happen with
in-built host objects.

It is just another host Function(-like) object that was created when the
script engine was invoked; its only specialty is that it _contains_ a call
to a built-in method. It is not the object referred to by `window' that
would be GC'd then, or the called built-in method, only one of `window''s
methods. So it is perfectly reasonable if that would be marked for GC
once there were no more references to it. It is therefore also perfectly
reasonable to assume that it would be restored if the document was
reloaded, which is in fact what happens.

BTW, I fail to find anything cynical about garbage collection other than
"Is it again /my/ turn to take out the garbage? (sigh)" ;-)


Regards,
PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top