On Nov 12, 5:35 pm, "Michael Geary" <[EMAIL PROTECTED]> wrote:
> That's invalid JavaScript. It's not the duplicate function name that makes
> it invalid, it's the fact that those are named functions at all. Even if you
> reduced the code to this, it would still be invalid:
>
>  $(document).ready(
>    function () {
>       field1.click(
>         function somename () {
>           // do something
>         }
>       );
>    }
>  );
>
> The only place you can define a named function is directly inside another
> function or in the global scope.

I'm not sure that's correct.  According to the ECMAScript spec section
10, an identifier is optional in a function expression.  They normally
aren't used because IE has a habit of making them global variables and
a reference to the function itself is available internally for the
purposes of recursion as arguments.callee.


> You can't define a named function inside a
> function call,

So:

  window.setTimeout(function(){...}, 20);

is a syntax error?


> as this code does, or inside any other code such as an "if"
> statement.

Trimming the OP's code to make it more  legible (to me) gives:

$(document).ready(
  function () {
     field1.click( function somename () { } );
     field2.click( function somename () { } );
  }
);

which can be made more general as:

function foo() {};
function bar0() {};
function bar1() {};

foo(
  function() {
    bar0( function somename() {} );
    bar1( function somename() {} );
  }
);

Which does not raise any exceptions in Safari 3.0.3 (or Firefox, Opera
or IE) but does in Safari 2.0.4.  Removing the identifier somename
prevents the error in Safari 2.0.4.

So I would say that it's a bug in Safari 2.0.4 that has been fixed in
version 3, but also that it is rather pointless to have included the
identifier in the first place.  If the functions need to refer to
themselves internally, the OP should use arguments.callee, otherwise
there is no need for the names at all.


--
Rob

Reply via email to