Closed Thread Icon

Topic awaiting preservation: Issues with assigning events inside objects... (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8472" title="Pages that link to Topic awaiting preservation: Issues with assigning events inside objects... (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Issues with assigning events inside objects... <span class="small">(Page 1 of 1)</span>\

 
Dracusis
Maniac (V) Inmate

From: Brisbane, Australia
Insane since: Apr 2001

posted posted 01-19-2003 03:03

Ok, this is going to be a lot easier with an example:

function myConstructor (var) {
this.alertValue = var
document.onmouseup = function() { alert(this.alertValue) }
}

The problem here is that the this keyword no longer refers to the the object when the event fires. It's as if I just typed the alert code into the main body of the script which kinda defeats the whole point of using objects. Is there any way I can get around this because I really need to set events from indide the object.

I'm completly stumped on this one -- as always, any help would be greatly apprecietd.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 01-19-2003 03:40

I encountered this recently while working on my ultra secret project. I was creating buttons and assigning their onclick events. I eventually used this strategy:

function myConstructor(var) {
var thebutton = create_button();
thebutton.associatedobject = this; // remember: objects are copied by reference, not value
thebutton.onclick = function() { alert(this.associatedobject.alertValue) }
}

Now, you seem to be assigning things to the document. The only issue with that is that if you create more than one object, it will override the settings of the previous one. But since that's already a problem in your existing code (document.onmouseup will be changed every time an instance of the object is made), I assume it's not a concern for you. So just try something like

function myConstructor (var) {
this.alertValue = var
document.associatedobject= this;
document.onmouseup = function() { alert(this.associatedobject.alertValue) }
}

There very well may be a more elegant solution than that.

Dracusis
Maniac (V) Inmate

From: Brisbane, Australia
Insane since: Apr 2001

posted posted 01-19-2003 04:00

"remember: objects are copied by reference, not value"

They are?... Well, that's handy to know.

"Now, you seem to be assigning things to the document. The only issue with that is that if you create more than one object, it will override the settings of the previous one."

That would also explain one of my earlier issues.. You just potentially saved me a lot of headaches. Not to worry, I'll still be needing to set up some events form inside objects. Thanks a heap for the work around, I sure as hell never would've figured that out.

Scott
Bipolar (III) Inmate

From: schillmania.com
Insane since: Jul 2002

posted posted 01-19-2003 09:58

Now it might be the time of day here (it's late ) - but couldn't you assign an event handler for an object that references a method within that object?

ie.

function asdf() {
this.eventHandler = function(e) {
// function within the scope of asdf();
this.event = e?e:window.event; // or whatever...
alert(this.event);
}
document.onmouseup = this.eventHandler;
}

Course I might be out of it also!

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 01-19-2003 16:51

Nice idea, but unfortunately, once you assign it to document.onmouseup, when it's called because the document was mouseuped, it runs as a method of the document object, rather than what it was originally defined as.

Scott
Bipolar (III) Inmate

From: schillmania.com
Insane since: Jul 2002

posted posted 01-20-2003 03:03

Ah-ha that makes sense .. I was looking over some stuff I'd written later on, and realized I hadn't followed that form for assigning event handers. Everything was = function() {something.blah(this)} or whatever.

OOP is some pretty cool stuff, particularly in Javascript, although the little things can really leave you scratching your brain from time to time

« BackwardsOnwards »

Show Forum Drop Down Menu