problems with objects, argument.caller and setTimeout

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • oliver

    #1

    problems with objects, argument.caller and setTimeout

    Hi,

    the structure of my source code is like this:
    <script>
    C = function(){
    //some initialization
    }

    C.prototype.sta rt = function{
    //some action
    setTimeout(argu ments.caller.ca llee, "200");
    }

    var obj = new C();
    obj.start();
    </script>

    This should execute the method start every 200 milisec, but (in some
    cases)
    it gives me an error because arguments.calle r is null. Is there a
    "direct" method to use setTimeout without this arguments.??? stuff?

    BTW: I'm working with IE Version >= 5.0

    Is there a better solution to get start work ?

    I am very grateful for every hint
    Oliver
  • Lasse Reichstein Nielsen

    #2
    Re: problems with objects, argument.caller and setTimeout

    biwoozle2004-javascript@yaho o.com (oliver) writes:
    [color=blue]
    > <script>[/color]
    Use:
    <script type="text/javascript">
    The type attribute is required in HTML 4.
    [color=blue]
    > C.prototype.sta rt = function{
    > //some action
    > setTimeout(argu ments.caller.ca llee, "200");[/color]

    The "arguments" object doesn't have a "caller" property. It does
    have "callee", but only in modern browsers.

    What are you trying to do? (You have to say, because when your code
    is wrong, we can't necessarily guess it from that)

    (And "200" should not be a string, but a number, so lose the quotes).
    [color=blue]
    > This should execute the method start every 200 milisec, but (in some
    > cases) it gives me an error because arguments.calle r is null.[/color]

    Only in some cases? Which cases? What do you change between the cases
    that work and the ones that doesn't?
    [color=blue]
    > Is there a "direct" method to use setTimeout without this
    > arguments.??? stuff?[/color]

    Your problem is to refer to the function itself inside it. It's not
    important that you use it with setTimeout.
    [color=blue]
    > BTW: I'm working with IE Version >= 5.0[/color]

    Which version? All of them?
    [color=blue]
    > Is there a better solution to get start work ?[/color]

    Try:
    ---
    C.prototype.sta rt = function start() {
    //some action
    setTimeout(star t, 200);
    };
    ---

    However, it's probably not what you want, because the calls to start
    from setTimeout/setInterval will not be as methods of the C object,
    i.e., the "this" value will not be set to that object while executing
    start. To achieve that, you'll have to wrap the call in a closure that
    knows the object:

    ---
    C.prototype.sta rt = function start() {
    //some action
    if (!this.timerId) {
    var _this = this;
    var recall = function() { _this.start(); };
    this.timerId = setInterval(rec all, 200);
    }
    ---
    or
    ---
    C.prototype.sta rt = function start() {
    //some action
    if (!this.recall) {
    var _this = this;
    this.recall = function() { _this.start(); };
    }
    setTimeout(this .recall, 200);
    }
    ---

    Good luck
    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: problems with objects, argument.caller and setTimeout

      Lasse Reichstein Nielsen wrote:[color=blue]
      > biwoozle2004-javascript@yaho o.com (oliver) writes:[color=green]
      >> C.prototype.sta rt = function{
      >> //some action
      >> setTimeout(argu ments.caller.ca llee, "200");[/color]
      >
      > The "arguments" object doesn't have a "caller" property. It does
      > have "callee", but only in modern browsers.[/color]

      Function objects have a `caller' property in JavaScript:

      function foo()
      {
      alert(foo.calle r); // alerts the source code of bar()
      }

      function bar() {
      foo();
      alert(bar.calle r); // alerts the whole source code or `null'
      }

      bar();


      PointedEars

      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: problems with objects, argument.caller and setTimeout

        Thomas 'PointedEars' Lahn <PointedEars@nu rfuerspam.de> writes:
        [color=blue]
        > Function objects have a `caller' property in JavaScript:[/color]

        In JavaScript (the Netscape implementation of ECMAScript), functions
        have an "arguments" property (deprecated in version 1.4) with a
        "caller" property (deprecated in 1.3).

        It seems "funvalue.calle r" was introduced in JavaScript 1.5 (in
        Mozilla, not in Netscape 4). The documentation is misleading, as it
        says "caller" is a property of "Function", while it is actually a
        property of "Function.proto type".

        For IE, JScript also has a "funvalue.calle r" property since version 2.
        <URL:http://msdn.microsoft. com/library/en-us/script56/html/js56jsprocaller .asp>

        It's still not part of ECMAScript, so other browsers probably doesn't
        understand it (Opera doesn't). If I don't say anything else, when I talk
        about language features, I'm talking about ECMAScript v3.

        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: problems with objects, argument.caller and setTimeout

          Lasse Reichstein Nielsen wrote:[color=blue]
          > Thomas 'PointedEars' Lahn <PointedEars@nu rfuerspam.de> writes:[color=green]
          >> Function objects have a `caller' property in JavaScript:[/color]
          >
          > In JavaScript (the Netscape implementation of ECMAScript), functions
          > have an "arguments" property (deprecated in version 1.4) with a
          > "caller" property (deprecated in 1.3).[/color]

          In my JavaScript 1.5 reference implementation (mozilla.org SpiderMonkey)
          the `arguments' property does not have a `caller' property, but Function
          objects have.
          [color=blue]
          > It seems "funvalue.calle r" was introduced in JavaScript 1.5 (in
          > Mozilla, not in Netscape 4).[/color]

          What's the problem?
          [color=blue]
          > The documentation is misleading, as it says "caller" is a property of
          > "Function", while it is actually a property of "Function.proto type".[/color]

          The Netscape JavaScript References are flawed not only in this regard.
          [color=blue]
          > [...]
          > It's still not part of ECMAScript, so other browsers probably doesn't
          > understand it (Opera doesn't). If I don't say anything else, when I
          > talk about language features, I'm talking about ECMAScript v3.[/color]

          I'll remember you the next time when you use e.g. the `window' object in
          this context.


          PointedEars

          Comment

          • Lasse Reichstein Nielsen

            #6
            Re: problems with objects, argument.caller and setTimeout

            Thomas 'PointedEars' Lahn <PointedEars@nu rfuerspam.de> writes:
            [color=blue][color=green]
            >> It's still not part of ECMAScript, so other browsers probably doesn't
            >> understand it (Opera doesn't). If I don't say anything else, when I
            >> talk about language features, I'm talking about ECMAScript v3.[/color]
            >
            > I'll remember you the next time when you use e.g. the `window' object in
            > this context.[/color]

            Hmm, I guess I'll have to rethink that statement :)

            Ok, at least ECMAScript was what I was thinking about when I said that there
            was no "caller" property.

            /L
            --
            Lasse Reichstein Nielsen - lrn@hotpop.com
            DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
            'Faith without judgement merely degrades the spirit divine.'

            Comment

            Working...