473,947 Members | 2,669 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.protot ype.open = function open()
{
alert("open");
}

function test()
{
var obj = new MyObject();
obj.open();
window.open("ht tp://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.protot ype.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 2770
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.protot ype.open = function open()
{
* * *alert("open");

}

function test()
{
* * *var obj = new MyObject();
* * *obj.open();
* * *window.open("h ttp://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.protot ype.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.protot ype.open = function open()
{
alert("open");
}

function test()
{
var obj = new MyObject();
obj.open();
window.open("ht tp://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.protot ype.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.protot ype.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(My Object.prototyp e, function open()
{
alert("open");
})
})
()

function defineMember(ob j, 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.ne t.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.protot ype.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
2209
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
2426
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 in prototype-based langauges as 'final or 'private' as you can in java so that some properties are 'hidden' from child objects. 2. Is there a way for objects to limit the delete function in javascript, or any prototype-based langauge, to...
2
8552
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 (sFoobar) { alert( sFoobar ); });
8
2069
by: Robert | last post by:
Hi, I can use "with" like this: function MyObject(message) { this.message = message; } function _MyObject_speak() {
12
2218
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 has been referred to as "junk" many times which is a strong opinion against the relatively high popularity of the library. I know popularity doesn't make something good.
11
1542
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 correctly). For example, when I do this, I use the keyword (?) "prototype": function Example() {
5
4196
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
1896
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" using the prototype property. I realize there are no classes in JS, that code therefore lives in objects instead of class definitions, and that "inheritance" must be achieved prototypically and not classically. That said, on with the code. Say I...
2
3181
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 i change something in php file using in ajax function.it not refreshed,means its shows the previous result it not get updated.i can't understand whats the prob.this is the code i m using: <? include("config.inc.php"); //error_reporting(0); ...
0
9982
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11571
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11155
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11344
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9886
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8254
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6113
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6331
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3541
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.