Connecting Tech Pros Worldwide Forums | Help | Site Map

how to get my javascript class?

Newbie
 
Join Date: Sep 2009
Posts: 7
#1: Sep 11 '09
Hi,
being quite new to javascript I'm scratching my head to the following issue, hope someone can help me...

I'm having a javascript "class" that looks like follows (just showing relevant parts)

Expand|Select|Wrap|Line Numbers
  1. function PlayControl (x) {
  2.    this.x = x;
  3. }
  4.  
  5. PlayControl.prototype.show = function() {
  6.    trace(this.x); 
  7.    setInterval (this.hide, 2000);
  8. }
  9.  
  10. PlayControl.prototype.hide = function() {
  11.    trace(this.x); 
  12. }
If I use this class like:
Expand|Select|Wrap|Line Numbers
  1. var control = new PlayControl(5);
  2. control.show();
the first trace prints "5" but the second trace (that is displayed after 2s) prints "undefined". Somehow, when the timer event invokes the hide function, "this" isn't the PlayControl object any more. My guess is that "this" represents the event target in this case, but how do I get to my PlayControl object in this scenario?

Thanks for any help!
/o

btw, I test this in Opera.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,648
#2: Sep 11 '09

re: how to get my javascript class?


setInterval() (along with some other methods*) alter the scope of this in the called function. in this case the first time this (in trace(this.x)) refers to control, the second time to window.

* most methods of window and every event handler
Newbie
 
Join Date: Sep 2009
Posts: 7
#3: Sep 11 '09

re: how to get my javascript class?


yes, that's what I suspected. But in the second case (in the hide method), how do I access the control object?
Thanks.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,648
#4: Sep 11 '09

re: how to get my javascript class?


Quote:

Originally Posted by olaalo View Post

But in the second case (in the hide method), how do I access the control object?

you can’t because the object control does not exist here (it’s a variable name!). nevertheless you can manually set this by the call() function.
Expand|Select|Wrap|Line Numbers
  1. setInterval.call(this, this.hide, 2000);
Newbie
 
Join Date: Sep 2009
Posts: 7
#5: Sep 11 '09

re: how to get my javascript class?


hmm, setInterval.call(null, this.hide, 2000) works fine (but I still get the window object in the hide function of course). setInterval.call(this, this.hide, 2000) doesn't get the hide function invoked at all...
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,648
#6: Sep 11 '09

re: how to get my javascript class?


until I find a better solution you can use a closure.
Expand|Select|Wrap|Line Numbers
  1. PlayControl.prototype.show = function() {
  2.    console.log(this.x);
  3.    var that = this;
  4.    var fn = function() { that.hide(); }
  5.    setInterval(fn, 2000);
  6. }
Newbie
 
Join Date: Sep 2009
Posts: 7
#7: Sep 11 '09

re: how to get my javascript class?


That works just fine! Thanks a lot!!
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,648
#8: Sep 11 '09

re: how to get my javascript class?


Quote:

Originally Posted by olaalo View Post

That works just fine!

I know, I made it work *gg* ;)
Newbie
 
Join Date: Sep 2009
Posts: 7
#9: Sep 14 '09

re: how to get my javascript class?


Hi again,
I've now got a related question (which clearly shows I haven't really understood how JS really works...) Anyway, the following code works fine:
Expand|Select|Wrap|Line Numbers
  1. function PlayControl (x)  {   
  2.  this.x = x;
  3.  var c = document.getElementById('control');
  4.  c.addEventListener("mousemove", this._dragSettingsDialog, false);
  5. }
  6.  
  7. PlaybackControl.prototype._dragSettingsDialog = function(evt)
  8. {
  9.   //do something
  10. }
I now would like (again) to access the PlayControl's private properties from the _dragSettingsDialog method. To do this I thought I should pass the PlayControl object as a parameter to the method, like:

Expand|Select|Wrap|Line Numbers
  1. PlaybackControl.prototype._dragSettingsDialog = function(evt, controlObject)
  2. {
  3.   controlObject.x;
  4. }
Is this the best way to do it? If so, what should the addEventListener method look like? I've tried things like c.addEventListener("mousemove", function (e) { this._dragSettingsDialog(e, this); }, false); without any luck.
Hope you can help me with this too.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,648
#10: Sep 14 '09

re: how to get my javascript class?


first, in Javascript there are no private properties (although you can mimic something similar)

addEventListener() is similar to setInterval() as it changes the scope of this to the element it is attached to. if you want to use this in the scope of your class, just do what you have done with setInterval()—use a closure.

to better understand these problems, read about Scope and Inheritance, as these are crucial to further understand Javascript.
Newbie
 
Join Date: Sep 2009
Posts: 7
#11: Sep 14 '09

re: how to get my javascript class?


Thanks for the pointer, I sure will read up on this (and yes, I'm just mimic private scoping here, or actually just trying to not pollute the global namespace with a bunch of properties).

I actually tried to use a closure like:
Expand|Select|Wrap|Line Numbers
  1. var that = this; 
  2. c.addEventListener("mousemove", that._dragSettingsDialog, false); 
but that doesn't make any difference at all, does the syntax need to be different?
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,648
#12: Sep 14 '09

re: how to get my javascript class?


Quote:

Originally Posted by olaalo View Post

I'm just mimic private scoping here, or actually just trying to not pollute the global namespace with a bunch of properties.

private properties and ‘not polluting the global namespace’ are two very different things.

Quote:

Originally Posted by olaalo View Post

I actually tried to use a closure like […]

that’s not a closure.

check out the references in this article
Newbie
 
Join Date: Sep 2009
Posts: 7
#13: Sep 14 '09

re: how to get my javascript class?


thanks for the link,
I'll look into this and hopefully I'll figure out how to make this work.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,648
#14: Sep 14 '09

re: how to get my javascript class?


unfortunately it’s not a piece of cake (when I think how long it took me to understand it), best is to try and code as much as possible.
Reply