setTimeout in object methods using closures (old problem?)

Discussion in 'Javascript' started by rain_c1@web.de, May 22, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. VK Guest

    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>
     
    VK, May 22, 2006
    #2
    1. Advertising

  3. Guest

    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, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Hill
    Replies:
    3
    Views:
    403
    Jim Hill
    Feb 12, 2007
  2. *Prot3anThr3ad*

    old repository for old C++ source code

    *Prot3anThr3ad*, Sep 29, 2006, in forum: C++
    Replies:
    6
    Views:
    381
    *Prot3anThr3ad*
    Oct 2, 2006
  3. John Henry
    Replies:
    24
    Views:
    1,016
    alex23
    May 30, 2008
  4. Kenneth McDonald
    Replies:
    5
    Views:
    324
    Kenneth McDonald
    Sep 26, 2008
  5. Andrew Poulos

    setTimeout and an object's methods

    Andrew Poulos, Mar 5, 2006, in forum: Javascript
    Replies:
    12
    Views:
    233
    Dr John Stockton
    Mar 13, 2006
Loading...

Share This Page