473,396 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

OOP and settimeout scope

Hi,

I've been searching google and the archives here but haven't found a
clear solution. I want to do something like the following

function C(){
this.message = 'hi';
}
C.prototype.set = function(){
setTimeout(this.talk, 4000);
};
C.prototype.talk = function(){
alert(this.message);
};

This doesn't do what I want, of course. I understand that when the
"talk" method runs the "this" won't be the same "this" as when the
"set" method runs. I would like the two "this" references to mean the
same instance of C and not the window. Is there a simple solution?

If closures are the solution is there a way to avoid moving the method
definititions into the constructor? That seems like cluttering the
constructor to me. But if that is the simplest it is ok too.

Thanks,
Peter

Jun 14 '06 #1
2 9442


pe**********@gmail.com wrote:
function C(){
this.message = 'hi';
}
C.prototype.set = function(){
setTimeout(this.talk, 4000);
};


A closure can help
C.prototype.set = function () {
var thisC = this;
setTimeout(function () { thisC.talk(); }, 4000);
};
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 14 '06 #2

Martin Honnen wrote:
pe**********@gmail.com wrote:
function C(){
this.message = 'hi';
}
C.prototype.set = function(){
setTimeout(this.talk, 4000);
};


A closure can help
C.prototype.set = function () {
var thisC = this;
setTimeout(function () { thisC.talk(); }, 4000);
};


Martin,

Thanks. This is a lot simpler than what I found in google. Just what I
was looking for.

(Totally unrelated but I just discovered that the google groups reply
button now automatically quotes.)

Peter

Jun 14 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
7
by: Antony Sequeira | last post by:
Hi While looking at some code I realized that the built in setTimeout function takes a string that is later evaluated in the original caller's context. How does one achieve something similar in...
3
by: Dica | last post by:
this works: document.all.style.visibility = 'hidden'; this doesn't setTimeout("document.all.style.visibility = 'hidden'",1000); looks like sResponseDivID isn't recognized. what's the problem?...
6
by: Brent | last post by:
Is there any obvious reason why there's no delay in the execution of setting the id.style.display when this function gets called? function Hide(divId,timeout) { var id =...
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
1
by: Joris Lambrecht | last post by:
Hi people, Please take a look at the issue i talk about below. (yes i do realise such functions are publicaly available) The page i'm using uses JSON defined array of image files with...
15
by: nikki_herring | last post by:
I am using setTimeout( ) to continuously call a function that randomly rotates/displays 2 images on a page. The part I need help with is the second image should rotate 3 seconds after the first...
7
by: -Lost | last post by:
I am calling setTimeout within the context of an object, and whilst this exists, it refuses to be passed along to the function I call. For example: $elemById('id').change = function() { // the...
4
by: Adam Risser | last post by:
Hi everyone, I am modifying the suckerfish dropdown code to use settimeout to have a slight pause before the menus disappear to make it more user friendly. I have hit a snag with the following...
7
by: Martin | last post by:
Can I have two setTimeouts running at the same time - with two different intervals? I want to start one timer and, before it times out, start another one I've tried this and they seems to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.