473,498 Members | 1,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Prototype and function name in Internet Explorer

Hi,

I thought I pretty much understood the whole prototype chain stuff,
but now I stumbled upon a difference between IE and Firefox, that
is totally confusing me.

An example....
************************
function MyObject()
{
}

MyObject.prototype.open = function open()
{
alert("open");
}

function test()
{
var obj = new MyObject();
obj.open();
window.open("http://www.google.com/", "Google");
}

************************

Notice that I name the function assigned to open. I do this because
I noticed that it can be convenient for stacktraces. Instead of
seeing an anonymous function, I can see the name of it.

Calling test in Firefox will give one "open" alert and one new window,
as I expected.
In Internet Explorer however I will get two "open" alerts!
Somehow the open method of the window object has been overwritten.
Changing the prototype method assignment to
MyObject.prototype.open = function()
"fixes" it, but I want to know why that name causes it to overwrite the
window open method.
And is there any other way to give a name to the method, so it is shown in
any stacktrace?

Kind regards,
Robert
Jan 18 '08 #1
3 2735
On Jan 18, 10:50*pm, Robert <rob...@nowhere.xwrote:
Hi,

I thought I pretty much understood the whole prototype chain stuff,
but now I stumbled upon a difference between IE and Firefox, that
is totally confusing me.

An example....
************************
function MyObject()
{

}

MyObject.prototype.open = function open()
{
* * *alert("open");

}

function test()
{
* * *var obj = new MyObject();
* * *obj.open();
* * *window.open("http://www.google.com/", "Google");

}

************************

Notice that I name the function assigned to open.
Function expressions don't need a name (it's optional), usually they
aren't used as they don't have any useful purpose.

I do this because
I noticed that it can be convenient for stacktraces. Instead of
seeing an anonymous function, I can see the name of it.
So you've found a use for naming anonymous functions. :-)
>
Calling test in Firefox will give one "open" alert and one new window,
as I expected.
In Internet Explorer however I will get two "open" alerts!
IE (erroneously) adds a named property to the global object as if
you'd declared the function with the name.

Somehow the open method of the window object has been overwritten.
Changing the prototype method assignment to
MyObject.prototype.open = function()
"fixes" it, but I want to know why that name causes it to overwrite the
window open method.
It is an IE bug.

var xx = function yy(){};
alert(typeof yy);

In the above IE shows "function", Firefox (and others) "undefined".
And is there any other way to give a name to the method, so it is shown in
any stacktrace?
Not as far as I know, you'll have to change the name.
--
Rob
Jan 18 '08 #2
Robert wrote:
Hi,

I thought I pretty much understood the whole prototype chain stuff,
but now I stumbled upon a difference between IE and Firefox, that
is totally confusing me.

An example....
************************
function MyObject()
{
}

MyObject.prototype.open = function open()
{
alert("open");
}

function test()
{
var obj = new MyObject();
obj.open();
window.open("http://www.google.com/", "Google");
}

************************

Notice that I name the function assigned to open. I do this because
I noticed that it can be convenient for stacktraces. Instead of
seeing an anonymous function, I can see the name of it.

Calling test in Firefox will give one "open" alert and one new window,
as I expected.
In Internet Explorer however I will get two "open" alerts!
Somehow the open method of the window object has been overwritten.
Changing the prototype method assignment to
MyObject.prototype.open = function()
"fixes" it, but I want to know why that name causes it to overwrite the
window open method.
Because

xpto = function abc(){...}

is the same as

function abc(){...}
xpto=abc;

That is, from the moment you write 'function abc', you're defining a
variable called 'abc' on that scope, which maps to that function. Since
you're doing that on top level code, it gets defined on the top level.
And the top level's scope is the object 'window'.

Firefox probably just disallows redefining window.open.
And is there any other way to give a name to the method, so it is shown in
any stacktrace?
Maybe you could do it inside some function, e.g.:

(function()
{
MyObject.prototype.open = function open()
{
alert("open");
}
})
()

?

Not that I'm very happy with that, since it probably makes you function
more heavy by including the enclosing function as scope.

If that doesn't do it, I don't think there's any alternative. On the
whole I'd not advise you to do this, as it requires repeating the
function name and that is prone to error. But you could always do it
differently, e.g.:

(function()
{
defineMember(MyObject.prototype, function open()
{
alert("open");
})
})
()

function defineMember(obj, fn)
{
// find the fn's name, by parsing its toString()
// if the engine doesn't supply it otherwise
var fname=...
obj[fname]=fn;
}

--
Posted via a free Usenet account from http://www.teranews.com

Jan 18 '08 #3
On Jan 18, 5:57 am, RobG <rg...@iinet.net.auwrote:
On Jan 18, 10:50 pm, Robert <rob...@nowhere.xwrote:

<snip>
>
So you've found a use for naming anonymous functions. :-)
Stack traces are useful things. Especially when using other libraries.
>
Calling test in Firefox will give one "open" alert and one new window,
as I expected.
In Internet Explorer however I will get two "open" alerts!

IE (erroneously) adds a named property to the global object as if
you'd declared the function with the name.
Jscript adds the function name to the enclosing Scope's Variable
object. The function is actually parsed as a function declaration
would be; I think it's a problem with the JScript parser.

Somehow the open method of the window object has been overwritten.
Changing the prototype method assignment to
MyObject.prototype.open = function()
"fixes" it, but I want to know why that name causes it to overwrite the
window open method.

It is an IE bug.

var xx = function yy(){};
alert(typeof yy);

In the above IE shows "function", Firefox (and others) "undefined".
And is there any other way to give a name to the method, so it is shown in
any stacktrace?

Not as far as I know, you'll have to change the name.
Yeah, bummer about that.

window.onerror isn't very useful either.
--
Rob
Jan 19 '08 #4

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

Similar topics

7
2184
by: Brian Genisio | last post by:
Hi all, Does anyone know of a way in IE to determine the prototype name of an object? For instance, in Mozilla, I can say: junk = document.getElementById("myID"); alert(junk.toString());
2
2372
by: Neil Morris | last post by:
I would like to know the benefits for javascripts' prototype objects as compared to say java class-based objects. The main points that I have on my mind are: 1. Is there a way to mark properties...
2
8510
by: Jason Keirstead | last post by:
Is there a way I can add a setter/getter to the HTMLElement prototype in internet explorer? This, for example, works fine in Mozilla: HTMLElement.prototype.__defineSetter__("foobar", function...
8
2035
by: Robert | last post by:
Hi, I can use "with" like this: function MyObject(message) { this.message = message; } function _MyObject_speak() {
12
2176
by: petermichaux | last post by:
Hi, I've been reading the recent posts and older archives of comp.lang.javascript and am surprised by the sentiments expressed about the prototype.js library for a few reasons: 1) The library...
11
1491
by: shypen42 | last post by:
Hi all, I'm very confused by the relation between "prototype" and that "Prototype.js" library that seems to be used quite a lot (not by knowledgeable people from this group if I understood...
5
4169
by: Gerry Vandermaesen | last post by:
Hi, Does anyone have a freely available JavaScript JSON stringifier. So far my search has been in vain, the one offered on http://www.json.org/json.js does not seem to work for me.
6
1854
by: burningodzilla | last post by:
Hi all - I'm preparing to dive in to more complex application development using javascript, and among other things, I'm having a hard time wrapping my head around an issues regarding "inheritance"...
2
3121
by: shivendravikramsingh | last post by:
hi friends, i m using a ajax function for retrieving some values from a database table,and display the values in required field,my prob is that the ajax function i m using is working f9 once,but if...
0
7126
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
7005
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
7168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7210
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5465
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,...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1424
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
659
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
293
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.