Property in prototype

R

Robert

Hi,

I would like to determine if a property is available in an Event prototype.
I tried doing this using:

if (Event.prototype.srcElement)

but I got an "Illegal operation on WrappedNative prototype object"
exception. Is there anyway to see if it already has been defined.

One other question I have is if the __defineGetter__ method is part of
some Javascript/ECMA Script version or if it is Mozilla proprietary.

Thanks!
Robert
 
M

Martin Honnen

Robert said:
One other question I have is if the __defineGetter__ method is part of
some Javascript/ECMA Script version or if it is Mozilla proprietary.

__defineGetter__ is implemented in the Spidermonkey engine that
implements JavaScript 1.5. That engine is used in Mozilla. So one could
say that __defineGetter__ is part of JavaScript 1.5 depending of what is
considered to define that version.
__defineGetter__ is not part of any existing ECMAScript specification
version.
 
M

Martin Honnen

R

Robert

Martin said:
Robert wrote:




Is that Firefox 1.03? Then you are probably running into a messy bug, a
workaround is described here:

No I use Firefox 1.04
Are you implying that Event.prototype.srcElement should work?
 
M

Martin Honnen

Robert said:
No I use Firefox 1.04
Are you implying that Event.prototype.srcElement should work?

I don't think it should give an error, of course natively srcElement is
not part of the Mozilla DOM so you would need to add your own script to
add that property.

What is the user agent string of Firefox 1.04, I have tested here with
Mozilla 1.7.7 (Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.7)
Gecko/20050414) and it does not give an error when accessing
Event.prototype.srcElement
but simply gives undefined.
 
R

Robert

Martin said:
I don't think it should give an error, of course natively srcElement is
not part of the Mozilla DOM so you would need to add your own script to
add that property.

Yes of course. I was expecting it to return undefined.
What is the user agent string of Firefox 1.04, I have tested here with
Mozilla 1.7.7 (Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.7)
Gecko/20050414) and it does not give an error when accessing
Event.prototype.srcElement
but simply gives undefined.

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.8) Gecko/20050511
Firefox/1.0.4

It seems mine is newer... So probably a regression. I will try to see if
they have a newer version available where they have fixed it again.
 
M

Martin Honnen

Robert wrote:

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.8) Gecko/20050511
Firefox/1.0.4

It seems mine is newer... So probably a regression. I will try to see if
they have a newer version available where they have fixed it again.

That is odd, I have now tried with Mozilla 1.7.8
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.8) Gecko/20050511
and do not get any error either.
 
R

Robert

Martin said:
Robert wrote:




That is odd, I have now tried with Mozilla 1.7.8
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.8) Gecko/20050511
and do not get any error either.

A restart has solved the problem.
Maybe the weather was too warm for the computer today :p
Anyway it works now :)
srcElement property being added if it was not already defined.

Thanks for trying to help me!
 
R

Robert

Robert said:
A restart has solved the problem.

I spoke too soon!
The problem only occurs after I have defined the srcElement.

So if I have executed the following code:

Event.prototype.__defineGetter__("srcElement", function()
{
var node = this.target;
while (node.nodeType != 1)
node = node.parentNode;
return node;
});

And later I do

if (Event.prototype.srcElement)

I will get the exception :(
 
Y

Yann-Erwan Perio

Robert wrote:

Hi,
The problem only occurs after I have defined the srcElement.

So if I have executed the following code:

Event.prototype.__defineGetter__("srcElement", function()
{
var node = this.target;
while (node.nodeType != 1)
node = node.parentNode;
return node;
});

And later I do

if (Event.prototype.srcElement)

I will get the exception :(


That's quite normal, you've actually just defined a getter for
Event.prototype, and then you *call* it in the "if" structure while you
should just check its existence; as a result, in the call, "this" refers
to the prototype itself, not any event, and calling the getter defined
for "target" in such a context makes it fail.

Be more defensive, with the srcElement getter

Event.prototype.__defineGetter__(
"srcElement",
function() {
var node=null;
if(this.constructor==Event) {
node = this.target;
while (node && node.nodeType != 1)
node = node.parentNode;
}
return node;
}
);

and with the way you infer the existence of a property

function isUndefined(obj, prop){
var isUndefined=true;
if(obj.__lookupGetter__){
if(typeof obj.__lookupGetter__(prop)!="undefined") {
isUndefined=false;
}
}
if(isUndefined) {
isUndefined=(typeof obj[prop]=="undefined");
}
return isUndefined;
}


HTH,
Yep.
 
R

Robert

Yann-Erwan Perio said:
Robert wrote:

Hi,




That's quite normal, you've actually just defined a getter for
Event.prototype, and then you *call* it in the "if" structure while you
should just check its existence; as a result, in the call, "this" refers
to the prototype itself, not any event, and calling the getter defined
for "target" in such a context makes it fail.

Ahhhhhhhh yes of course!
Thank you. I understand it now and your examples were helpful too.
 

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
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top