Re: a question about jibbering.com FAQ essay on Javascript closures
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.javascript archive

Re: a question about jibbering.com FAQ essay on Javascript closures

From: Thomas 'PointedEars' Lahn <PointedEars@web.de>
Date: Sat Nov 12 2005 - 21:09:41 CET

lkrubner@geocities.com wrote:

> | However, code could call another function that returned a reference to
> | an inner function object, with that inner function object being passed
> | by reference to the setTimeout function. The parameters to be used for
> | the execution of the inner function are passed with the call to the
> | function that returns it. setTimout executes the inner function without
> | passing arguments but that inner function can still access the
> | parameters provided by the call to the outer function that returned
> | it:- "
>
> um, isn't this the same as handing settimeout an object?

I don't think so.

> I don't get the difference. Couldn't I create an object like this:
>
> var myObject = new Object();
>
> 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);

You could, but that would probably not have a usable result. Since
setTimeout() is a host object's method and it's behavior is not clearly
specified, there are the following possibilities:

a) As `myObject' is a reference to an Object object and not to a Function
   object, or as the UA's AOM does not support function references here,
   the value of `myObject' (the object reference) is type-converted to
   string. Unless the object defined another toString() method, this
   would result in

     hideMenu = setTimeout("[Object object]", 500);

   and the like. Since `[Object object]' is syntactically incorrect code,
   a runtime error would occur.

b) As `myObject' is neither a string nor a Function object reference,
   an exception would be thrown.

c) none of the above (undefined behavior).

HTH

PointedEars
Received on Mon Nov 21 03:27:09 2005