lkrubner@geocities.com wrote:
<snip>
> ... . If a piece of code wants to use setTimeout it calls
> the setTimeout function and passes a reference to a
> function object as the first argument and the millisecond
> interval as the second, but a reference to a function object
> cannot provide parameters for the scheduled execution of
> that function.
<snip>
> um, isn't this the same as handing settimeout an object?
In javascript functions are objects, so yes it is exactly the same as
handing setTimeout an object. Function objects are executable and
setTimeout schedules the execution of functions passed to it as
arguments so it is quite important that what is passed is executable,
and so is a function object.
Because setTimeout implementations also accept strings as their first
argument (or exclusively accept strings as their first argument)
non-function arguments will be type-converted into strings (and where
function arguments are not supported then even they will be
type-converted into strings). (There is no type-conversion mechanism
that can convert a non-function into a function; that would be
chaotic.).
> I don't get
> the difference. Couldn't I create an object like this:
>
> var myObject = new Object();
Objects created with - new Object() - cannot be exeucted.
> and then give it properties like this:
>
> myObject.param1 = 3;
> myObject.param2 = "The world is basically a good place.";
> myObject.param3 = new Array("hello", "goodbye", "no way");
>
> And then couldn't I give this to setTimeOut?
>
> hideMenu = setTimeout(myObject, 500);
>
> Is this different than what they suggest in the essay,
> with this bit of code:
<snip>
Using closures to pass arguments to functions executed with setTiemout
is not the only way of achieving that goal, they are just one way of
ding so. But passing plain object references is not one of the viable
alternatives.
Richard.
Received on Mon Nov 21 03:28:01 2005