473,396 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

object.prototype question

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".

Mar 15 '06 #1
3 4175
VK

steve dp wrote:
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.

Mar 15 '06 #2
steve dp wrote:
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;">

--
Rob
Mar 15 '06 #3
RobG wrote:
steve dp wrote:
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.


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
Mar 16 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
2
by: Richard Trahan | last post by:
In the 1.5 Guide, Chapter 8 (Details of the Object Model), last sentence: "The dennis object does not inherit this new property". I don't understand why this is so. A few pages earlier, it says...
8
by: Steve Neill | last post by:
Can anyone suggest how to create an arbitrary object at runtime WITHOUT using the deprecated eval() function. The eval() method works ok (see below), but is not ideal. function Client() { }...
7
by: Florian Loitsch | last post by:
hi, in section 10.1.8 (Arguments Object) it is stated that the "internal ] property of the arguments object is the orginal Object prototype object, the one that is the *initial* value of...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
6
by: Jake Barnes | last post by:
I was just reading this article on Ajaxian: http://ajaxian.com/archives/show-love-to-the-object-literal This is a newbie question, but what is the object literal? I thought it was like an...
45
by: bigdadro | last post by:
I've created a new class using prototype.js. After I make the ajax.request all references to this.myClassMethodorVariable are lost. Does the ajax method blow out the object persistance? I'm fairly...
6
by: Murray Hopkins | last post by:
Hi. THE QUESTION: How do I get a reference to my Object when processing an event handler bound to an html element ? CONTEXT: Sorry if it is a bit long. I am developing a JS calendar tool....
0
by: Stodge | last post by:
Hi folks, new to Boost Python and struggling to build a prototype at work. I thought I'd start with a conceptual question to help clarify my understanding. I already have a basic prototype working...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.