On Feb 8, 8:45 am, mcot <[email protected]> wrote:
> I see this code in the prototype lib and just wondering whats going on
> (see my comments for specific questions):
>
> Object.extend(Function.prototype, (function() {
>
> ...
> ...
> ...
>
> function delay(timeout) {
> var __method = this, args = slice.call(arguments, 1);
> timeout = timeout * 1000
> return window.setTimeout(function() {
> return __method.apply(__method, args); // where does this get
> "returned to"?
Given that setTimeout is part of DOM 0 and it has no specification,
how it works must be determined by observation.
A return statement causes a function to cease execution and return a
value to the caller. Using a small test case:
function foo(x){
alert(x + '\n' + arguments.callee.caller);
}
foo.delay(2, 'whatever');
shows:
whatever
function () {
return __method.apply(__method, args);
}
So the caller is the anonymous function passed to setTimeout in IE,
Chrome and Firefox.
A return statement may have a useful purpose in some other browser,
but it appears useless and can be replaced by a simple call (by
removing the return keyword). I expect it is there as a result of copy
and paste from other, nearby functions that have a similar pattern.
> /*
> According to mozilla docs (https://developer.mozilla.org/en/DOM/
> window.setTimeout) setTimeout should just return a unique id to use
> with clearTimeout() and all of my tests show this to be true.
> */
Yes, that is what setTimeout returns. But that is not where the return
value from the function called by setTimeout goes.
--
Rob
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]