By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,213 Members | 2,172 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,213 IT Pros & Developers. It's quick & easy.

problems with objects, argument.caller and setTimeout

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
bi*********************@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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #2

P: n/a
Lasse Reichstein Nielsen wrote:
bi*********************@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
Jul 23 '05 #3

P: n/a
Thomas 'PointedEars' Lahn <Po*********@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.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #4

P: n/a
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn <Po*********@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
Jul 23 '05 #5

P: n/a
Thomas 'PointedEars' Lahn <Po*********@nurfuerspam.de> writes:
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.


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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.