Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

problems with objects, argument.caller and setTimeout

1 view
Skip to first unread message

oliver

unread,
Jul 15, 2004, 6:24:01 AM7/15/04
to
Hi,

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

C.prototype.start = function{
//some action
setTimeout(arguments.caller.callee, "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.caller 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

unread,
Jul 15, 2004, 6:53:18 AM7/15/04
to
biwoozle200...@yahoo.com (oliver) writes:

> <script>
Use:
<script type="text/javascript">
The type attribute is required in HTML 4.

> C.prototype.start = function{
> //some action
> setTimeout(arguments.caller.callee, "200");

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).

> This should execute the method start every 200 milisec, but (in some
> cases) it gives me an error because arguments.caller is null.

Only in some cases? Which cases? What do you change between the cases
that work and the ones that doesn't?

> Is there a "direct" method to use setTimeout without this
> arguments.??? stuff?

Your problem is to refer to the function itself inside it. It's not
important that you use it with setTimeout.

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

Which version? All of them?

> Is there a better solution to get start work ?

Try:
---
C.prototype.start = function start() {
//some action
setTimeout(start, 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.start = function start() {
//some action
if (!this.timerId) {
var _this = this;
var recall = function() { _this.start(); };
this.timerId = setInterval(recall, 200);
}
---
or
---
C.prototype.start = 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 - l...@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'

Thomas 'PointedEars' Lahn

unread,
Jul 17, 2004, 9:31:15 PM7/17/04
to
Lasse Reichstein Nielsen wrote:

> biwoozle200...@yahoo.com (oliver) writes:
>> C.prototype.start = function{
>> //some action
>> setTimeout(arguments.caller.callee, "200");
>
> The "arguments" object doesn't have a "caller" property. It does
> have "callee", but only in modern browsers.

Function objects have a `caller' property in JavaScript:

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

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

bar();


PointedEars

Lasse Reichstein Nielsen

unread,
Jul 18, 2004, 7:09:18 AM7/18/04
to
Thomas 'PointedEars' Lahn <Point...@nurfuerspam.de> writes:

> Function objects have a `caller' property in JavaScript:

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.caller" 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.prototype".

For IE, JScript also has a "funvalue.caller" 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.

Thomas 'PointedEars' Lahn

unread,
Jul 18, 2004, 9:05:27 AM7/18/04
to
Lasse Reichstein Nielsen wrote:
> Thomas 'PointedEars' Lahn <Point...@nurfuerspam.de> writes:
>> Function objects have a `caller' property in JavaScript:
>
> 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).

In my JavaScript 1.5 reference implementation (mozilla.org SpiderMonkey)
the `arguments' property does not have a `caller' property, but Function
objects have.

> It seems "funvalue.caller" was introduced in JavaScript 1.5 (in
> Mozilla, not in Netscape 4).

What's the problem?

> The documentation is misleading, as it says "caller" is a property of
> "Function", while it is actually a property of "Function.prototype".

The Netscape JavaScript References are flawed not only in this regard.

> [...]


> 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.

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


PointedEars

Lasse Reichstein Nielsen

unread,
Jul 18, 2004, 9:20:01 AM7/18/04
to

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.

0 new messages