.addEventListener() not finding the right object method

L

Liming

Hello all,

I have a custom class object with some prototype methods like
setKeyDownEvent, etc.

The problem is on my webpage, after I instantiate the class, I try to
do .addEventLister() to a textarea by attaching my custom class
object's setKeyDownEvent,etc to it. Long story short, here is an
example of my code.

----------------

var myownobject ;

window.onload = init;

function init()
{
myobj= = new MyObject();
initListeners();
}

function initListener()
{
if (navigator.userAgent.indexOf("Safari") > 0) {

myobj.getTextarea().addEventListener("keydown",myobj.setKeyDownEvent,true);

myobj.getTextarea().addEventListener("keyup",myobj.setKeyUpEvent,true);
} else if (navigator.product == "Gecko") {

myobj.getTextarea().addEventListener("keypress",myobj.setKeyDownEvent,false);

myobj.getTextarea().addEventListener("keyup",webcmd.setKeyUpEvent,false);

} else {

myobj.getTextarea().attachEvent('onkeydown',myobj.setKeyDownEvent,false);

myobj.getTextarea().attachEvent('onkeyup',myobj.setKeyUpEvent,false);
}
}

MyObject is a custom class I have and the problem is inside its
MyObject.setKeyDown mehtod, I'm calling other methods of MyObject by
doing like

Myobject.prototype =
{
setKeyDownEvent : function()
{
this.method2()
}
}
....

so when a say key event is pressed, firefox say this.mehtod2() is not a
function (i know this module works btw, because before I did the
traditional way like so, it worked)

<script ...>
...
function kd(e)
{
myobj.setKeyUpEvent(e)
}
function ku()
{
myobj.setKeyUpEvent(e)
}
</script>
<textarea id="mytextarea" oonKeyPress="kd(event);" onKeyUp="ku(event)"
/>




so I'm thinking, this's try to find "this" inside window object instead
of MyObject.
 
L

Liming

and oh btw? this.method2() is just an example here, it's there in the
function, trust me.. like

Myobject.prototype =
{

mehtod2:function{
},
....
}

thanks again.
 
M

Michael Winter

I have a custom class object with some prototype methods like
setKeyDownEvent, etc.

The problem is on my webpage, after I instantiate the class, I try to
do .addEventLister() to a textarea by attaching my custom class
object's setKeyDownEvent,etc to it.

I answered a question on this same topic yesterday. See
<URL:http://groups.google.co.uk/group/co...hread/9b041b9103b6de0f/2065870c97d74b50?tvc=2>.

[snip]
if (navigator.userAgent.indexOf("Safari") > 0) {

Don't use browser detection. See
<URL:http://www.jibbering.com/faq/#FAQ4_26> and the relevant links.

[snip]

Mike
 
L

Liming

Mike,

That's a GREAT tutorial, wow, that was good. But I do have this
question...

You have the var instance = this; defined in the class (private
variable).. and such it's accessible to the inner functions of the
"constructor" class ONLY.

what if you are calling from a method of .prototype.mehtod()? In this
case, i wont' have access to the private instance variable "instance".
What's the best way to do this? Move this prototype method into the
class "consturctor" as an inner function?

I just recently picked up javascript, have a lot stupid questions,
please bear with me.
 
M

Michael Winter

On 26/08/2005 19:58, Liming wrote:

[snip]
You have the var instance = this; defined in the class (private
variable).. and such it's accessible to the inner functions of the
"constructor" class ONLY.

what if you are calling from a method of .prototype.mehtod()? In this
case, i wont' have access to the private instance variable "instance".
What's the best way to do this? Move this prototype method into the
class "consturctor" as an inner function?

[snip]

You could do that, though it's not the only solution. An alternative is
to create a property on the element that references your object. When
your listeners are executed, the this operator will reference the element:

function MyObject() {
/* ... */
}
MyObject.prototype.listener = function() {
/* Use 'this.object' to get reference to object instance. */
};

var o = new MyObject();

myElement.object = o;
myElement.addEventListener('...', o.listener, false);

Hope that helps,
Mike
 

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,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top