self.parent vs. parent?

David Faure faure at kde.org
Wed Mar 9 23:08:21 GMT 2005


On Wednesday 09 March 2005 17:35, Jason Keirstead wrote:
> On Wednesday 09 March 2005 11:09 am, David Faure wrote:
> > A solution is obviously to use another property name here. If you name it
> > nodeParent then you don't run the risk of conflicting with window.parent.
> 
> Unfortunately this is a third-party JS library :/
> 
> > Really? I'm not sure I would expect that :)
> > This function is really related to the node....
> 
> I don't know what should happen here as far as the W3C specs, but from my 
> experimenting, the scope of the function is certainly *not* inherited from 
> the node in Gecko or IE at all - KHTML is the only browser carrying the scope 
> that I have access to run a test case on.
> 
> For example, try this in Gecko and IE and KHTML:
> 
> document.body.onclick = function() {
> 	alert ( innerHTML );
> }
> 
> Only in KHTML will you get the innerHTML of the node. In other browsers, you 
> get 'innerHTML is not defined' or other simmilar messages.

Indeed. Very surprising. "this" is the node, but it's not in the scope.
But only in the above case, not always. See this testcase:

<form name="f">
<input type="text" id="inp" value="first"/>
</form>
<script>
         document.getElementById('inp').onclick = function() {
                 alert( this );
                 alert( this.tagName );
                 alert( tagName );
         }
</script>

<input type="text" id="inp2" value="second" onclick="alert( this ); alert( this.tagName ); alert( tagName );"/>

This shows (in Moz and IE) that there is a difference between those two ways.
Both onclick event handlers have "this" set to the element being clicked,
but only the second input has "this" in the scope (i.e. tagName resolves).
In the first case (the function()), "this" isn't in the scope.

Harri: any idea how we can differ between those two cases? Does this scope difference
make any sense to you at the KJS level?

All I know is that everything goes through JSEventListener::handleEvent, I don't see
a difference between the two ways at that level....

-- 
David Faure, faure at kde.org, sponsored by Trolltech to work on KDE,
Konqueror (http://www.konqueror.org), and KOffice (http://www.koffice.org).




More information about the kfm-devel mailing list