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) - function PlayControl (x) {
-
this.x = x;
-
}
-
-
PlayControl.prototype.show = function() {
-
trace(this.x);
-
setInterval (this.hide, 2000);
-
}
-
-
PlayControl.prototype.hide = function() {
-
trace(this.x);
-
}
If I use this class like: - var control = new PlayControl(5);
-
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.
13 2092 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
yes, that's what I suspected. But in the second case (in the hide method), how do I access the control object?
Thanks.
@olaalo
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. - setInterval.call(this, this.hide, 2000);
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...
until I find a better solution you can use a closure. - PlayControl.prototype.show = function() {
-
console.log(this.x);
-
var that = this;
-
var fn = function() { that.hide(); }
-
setInterval(fn, 2000);
-
}
That works just fine! Thanks a lot!!
@olaalo
I know, I made it work *gg* ;)
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: - function PlayControl (x) {
-
this.x = x;
-
var c = document.getElementById('control');
-
c.addEventListener("mousemove", this._dragSettingsDialog, false);
-
}
-
-
PlaybackControl.prototype._dragSettingsDialog = function(evt)
-
{
-
//do something
-
}
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: - PlaybackControl.prototype._dragSettingsDialog = function(evt, controlObject)
-
{
-
controlObject.x;
-
}
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.
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.
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: - var that = this;
-
c.addEventListener("mousemove", that._dragSettingsDialog, false);
but that doesn't make any difference at all, does the syntax need to be different?
@olaalo
private properties and ‘not polluting the global namespace’ are two very different things. @olaalo
that’s not a closure.
check out the references in this article
thanks for the link,
I'll look into this and hopefully I'll figure out how to make this work.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Alex Fitzpatrick |
last post by:
Just by way of introduction, I'm currently the principal developer and
maintainer of the a JavaScript editor plug-in for Eclipse.
https://sourceforge.net/projects/jseditor/ The plug-in as it...
|
by: bin_P19 P |
last post by:
the code i have got is as follows and now im stuck
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Shopping...
|
by: chrisdude911 |
last post by:
how do i add video into a javascript web page with my own custom
buttons?
|
by: Greg |
last post by:
I'm guessing the problem I'm having has something to do with Master Pages or
DetailsView because the exact same code works fine on a page without a Master
Page and DetailsView controls.
The...
|
by: Wm.M.Thompson |
last post by:
For a computer programmer JavaScript is not difficult. It is pretty
easy to look at some code for the first time and figure out what is
going on.
This is especially true if you have gratuated...
|
by: Tom Cole |
last post by:
I'm working on a small Ajax request library to simplify some tasks
that I will be taking on shortly. For the most part everything works
fine, however I seem to have some issues when running two...
|
by: rajesh |
last post by:
< script language = javascript c =...
|
by: pbmods |
last post by:
VARIABLE SCOPE IN JAVASCRIPT
LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN )
PREREQS: VARIABLES
First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
|
by: bhanubalaji |
last post by:
hi,
I am unable to disable the text(label) in javascript..it's working fine with IE,but i am using MOZILLA..
can any one help regarding this..
What's the wrong with my code?
I am...
|
by: sorobor |
last post by:
dear sir ..
i am using cakephp freamwork ..By the way i m begener in php and javascript ..
My probs r bellow
I made a javascript calender ..there is a close button ..when i press close button...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |