Mouse events, classes, handlers

F

fuubaa

This below would be my ideal code.... if it worked.

<html><head><script>

function mouseDownClass(o){

this.o = o;

this.handleMouseUp = function() {
alert(this);

}
//treating this section as the constructor (which sets up the mouseup
listener as the method of an instantiation, ideally)
document.body.addEventListener("mouseup",this.handleMouseUp,false);

}

</script></head><body>

<button id="b1" onmousedown="new mouseDownClass(this,event)">Down
-&gt; Up</button>

</body></html>

alert() is showing 'this' to as: [object HTMLBodyElement] whereas i'm
looking for it to show [object Object], (On Firefox at least),
referring to the object instantiated when the mouse goes down on the
button. I'd appreciate anyone who can help me here?
Thanks in advance
Jody
 
T

Thomas 'PointedEars' Lahn

fuubaa said:
This below would be my ideal code.... if it worked.

Since it is fantasy code, it cannot work.
<html><head><script>

Not at all Valid. said:
function mouseDownClass(o){

There are no classes in the used ECMAScript implementation, BTW.
this.o = o;

this.handleMouseUp = function() {
alert(this);

}
//treating this section as the constructor

Nonsense. `mouseDownClass' is the constructor of the object created with
the NewExpression.
(which sets up the mouseup listener as the method of an instantiation, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ideally)

Nonsense. It adds the Function object referred to by this.handleMouseUp
(that could have been any other Function object as well, it would not have
make a difference) as event listener for the `mouseup' event.
document.body.addEventListener("mouseup",this.handleMouseUp,false);

}

</script></head><body>

<button id="b1" onmousedown="new mouseDownClass(this,event)">Down
-&gt; Up</button>

So when the `mousedown' event occurs for the `button' element as event
target, you create a new object. Which is completely unnecessary.

And said:
alert() is showing 'this' to as: [object HTMLBodyElement]

And rightfully so. You added the event listener to the element
represented by the element object referred to with `document.body'.
When its `mouseup' event occurs, the Function code is called by
this object. And `this' refers to the caller in function code.
whereas i'm looking for it to show [object Object],

function ...(...)
{
var me = this;

this.handleMouseUp = function()
{
alert(me);
}

...
}

Note that this creates a closure involving DOM objects which is
prone to the IE memory leak problem.


PointedEars
 

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

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top