Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #25944 > unrolled thread

event listener, objects and this

Started bypentapus <pentapus@example.com>
First post2014-10-20 21:33 -0400
Last post2014-10-21 13:41 +0700
Articles 2 — 2 participants

Back to article view | Back to comp.lang.javascript


Contents

  event listener, objects and this pentapus <pentapus@example.com> - 2014-10-20 21:33 -0400
    Re: event listener, objects and this JJ <jj4public@vfemail.net> - 2014-10-21 13:41 +0700

#25944 — event listener, objects and this

Frompentapus <pentapus@example.com>
Date2014-10-20 21:33 -0400
Subjectevent listener, objects and this
Message-ID<m24d5j$b2h$1@news.albasani.net>
I'm writing an object for an HTML5 audio player

in my object initialization code I have this:

function audioVideoPlayer(id){
	this.id = id;
	this.playing = false;
	this.endOfTrack =  endOfTrack;
...

	this.player.addEventListener("ended",this.endOfTrack);

That calls the endOfTrack function but the endOfTrack function believes 
"this" refers to the audio element, not to the object I wrote. How do I 
properly call the endOfTrack method?

Nice to see this group is still kicking and doing good work.


-- 
pentapus

[toc] | [next] | [standalone]


#25945

FromJJ <jj4public@vfemail.net>
Date2014-10-21 13:41 +0700
Message-ID<1hlyyopchglha$.1frlkpoakgpao$.dlg@40tude.net>
In reply to#25944
On Mon, 20 Oct 2014 21:33:23 -0400, pentapus wrote:
> I'm writing an object for an HTML5 audio player
> 
> in my object initialization code I have this:
> 
> function audioVideoPlayer(id){
> 	this.id = id;
> 	this.playing = false;
> 	this.endOfTrack =  endOfTrack;
> ...
> 
> 	this.player.addEventListener("ended",this.endOfTrack);
> 
> That calls the endOfTrack function but the endOfTrack function believes 
> "this" refers to the audio element, not to the object I wrote. How do I 
> properly call the endOfTrack method?
> 
> Nice to see this group is still kicking and doing good work.

Use a bound event handler.

  this.player.addEventListener("ended", this.endOfTrack.bind(this));

Assuming that "this" in that context, is the object you want.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web