setTimeout in object methods using closures (old problem?)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rain_c1@web.de

    #1

    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.func 3());
    };
    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.func 3());
    };
    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.func 3());
    };
    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.func 3());
    };
    this.func3 = function() {
    return text;
    };
    };
    return _class;
    })(); //simultaneously define and call (one-off)!
    test = new Test('hello world');
    test.func1();

  • VK

    #2
    Re: setTimeout in object methods using closures (old problem?)


    rain_c1@web.de wrote:[color=blue]
    > 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.[/color]

    Something similar (on the first look, but not the fact) was at
    <http://groups.google.c om/group/comp.lang.javas cript/browse_frm/thread/bf33c7bbb9f699f 9/1fa2877e1219f70 4>

    Comment

    • rain_c1@web.de

      #3
      Re: setTimeout in object methods using closures (old problem?)

      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.fun c3()); // <== 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

      Comment

      Working...