Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > setTimeout in object methods using closures (old problem?)

Reply
Thread Tools

setTimeout in object methods using closures (old problem?)

 
 
rain_c1@web.de
Guest
Posts: n/a
 
      05-22-2006
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();

 
Reply With Quote
 
 
 
 
VK
Guest
Posts: n/a
 
      05-22-2006

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>

 
Reply With Quote
 
 
 
 
rain_c1@web.de
Guest
Posts: n/a
 
      05-22-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
Using object methods of first module in methods of second module Nikita Petrov Ruby 2 04-06-2008 08:49 PM
setTimeout and an object's methods Andrew Poulos Javascript 12 03-13-2006 11:28 PM
using setTimeout when using prototype James Black Javascript 7 02-03-2006 03:11 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57