By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,619 Members | 1,861 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,619 IT Pros & Developers. It's quick & easy.

Prototype and function name in Internet Explorer

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.