473,395 Members | 1,869 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,395 software developers and data experts.

setTimeout in object methods using closures (old problem?)

hi,

i think this is a little exercise for real experts, but i suffer from
real headaches because of it... :-\

i have an object method (method1), that calls setTimeout with an other
method (method2) as parameter which calls further another method
(method3), but i do not know how to realize it.
it is important to use object context and not static members, because
method3 uses a member (more precisely a constructor parameter) that
should be private if possible.

-----------
1. try:
-----------
function Test(text) {
this.func1 = function() {
setTimeout(this.func2, 100);
};
this.func2 = function() {
alert(this.func3());
};
this.func3 = function() {
return text;
};
}
test = new Test('hello world!');
test.func1();
-----------
error : this.func3 is not a function. ok i understand, due to
setTimeout we loose the execution context. i tried several other ways
that i will post at the end, if you have any way to get this to work
without changing the signature of any method i would be very glad!!!

thanks,
- rainer

--------------------
2. try:
using a closure to return a reference to func2 for saving execution
context.
==> same result: "this.func3 is not a function"
--------------------
function Test(text) {
this.funcX = function() {
return this.func2;
};
this.func1 = function() {
setTimeout(this.funcX(), 100);
};
this.func2 = function() {
alert(this.func3());
};
this.func3 = function() {
return text;
};
}
test = new Test('hello world!');
test.func1();

---------------------
3. try:
using prototype, little difference : member text now needs to be
public, not so nice!
==> same result: "this.func3 is not a function"
---------------------

function Test(text) {
this.text = text;
}
Test.prototype.funcX = function() {
return this.func2;
};
Test.prototype.func1 = function() {
setTimeout(this.funcX(), 100);
};
Test.prototype.func2 = function() {
alert(this.func3());
};
Test.prototype.func3 = function() {
return this.text;
};
test = new Test('hello world!');
test.func1();

--------------------------------
4. try:
using real object closure
==> same result: "this.func3 is not a function"
--------------------------------
var Test = (function(){
function _class(text) {
this.funcX = function() {
return this.func2;
};
this.func1 = function() {
setTimeout(this.funcX(), 100);
};
this.func2 = function() {
alert(this.func3());
};
this.func3 = function() {
return text;
};
};
return _class;
})(); //simultaneously define and call (one-off)!
test = new Test('hello world');
test.func1();

May 22 '06 #1
2 10381
VK

ra*****@web.de wrote:
hi,

i think this is a little exercise for real experts, but i suffer from
real headaches because of it... :-\

i have an object method (method1), that calls setTimeout with an other
method (method2) as parameter which calls further another method
(method3), but i do not know how to realize it.
it is important to use object context and not static members, because
method3 uses a member (more precisely a constructor parameter) that
should be private if possible.


Something similar (on the first look, but not the fact) was at
<http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/bf33c7bbb9f699f9/1fa2877e1219f704>

May 22 '06 #2
thank you very much, this post solved my problem:

var Test = (function(){
function _class() {
var _self = this; // <== this is solution part one
this.func1 = function() {
setTimeout(this.func2, 100);
};
this.func2 = function() {
alert(_self.func3()); // <== this is solution part two
};
this.func3 = function() {
return 'hello world';
};
};
return _class;
})(); //simultaneously define and call (one-off)!

test = new Test();
test.func1();

thanks for the quick reference,
- rainer

May 22 '06 #3

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

Similar topics

6
by: Joe Kelsey | last post by:
When you use addEventListener (or addEvent in IE) to call an object method, does it call it with the correct this parameter? The ECMAScript reference has a lot to say about the caller using...
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) {...
6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
2
by: Athanasius | last post by:
Could someone shed some light as to why the following setTimeout function will not work on the Mac IE5.2? It does however work on PC(Forefox,Netscape,IE) & Mac(Safari,Firefox). Here is the script,...
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. ...
11
by: emailscotta | last post by:
Below I declared a basic object literal with 2 methods. The "doSomething" method is call from the "useDoSomething" method but the call is only sucessful if I use the "this" keyword or qualify the...
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...
3
by: phocis | last post by:
I wrote a pair of functions to enable scoped or referenced setTimeout calls. I did this because I have an object factory that creates multiple objects and those objects need to delay a few calls on...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.