setTimeout in object methods using closures (old problem?)

R

rain_c1

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();
 
V

VK

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..._frm/thread/bf33c7bbb9f699f9/1fa2877e1219f704>
 
R

rain_c1

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top