Re: <variable>=function(){...}



On 13/10/2005 21:17, Jim Moon wrote:

I'm curious about this syntax:
<variable>=function(){...}

What you have there is a function expression.

Function declarations:

  function identifier() {
  }

are parsed into function objects before execution begins[1]. This is why one can call a declared function before its definition in the source.

  myFunction();  /* No problem calling this here */

  function myFunction() {
    /* ... */
  }

Function expressions are different in that they only create function objects /if/ they are evaluated. This means that control structures can prevent them from being created at all, or repeatedly evaluate the expression to create several distinct function objects from the same code.

  var myFunction;

  if(false) {
    myFunction = function() { /* ... */ };
  }

Here, a function object won't be created because the code branch containing the function expression will never be entered.

This feature affords tremendous flexibility to the author.

Like function declarations, function expressions can be given identifiers.

  var myVariable = function myFunction() {};

However, the identifier is only for use within the function itself; it allows the function to refer to itself.

[snip]

Mike


[1] To be more precise, function declarations are parsed when their containing execution context is entered. That is, global function declarations are parsed immediately, but inner functions are only parsed when their outer function is called. Moreover, they are reparsed on each invocation of that outer function.

      function myGlobalFunction() {

        function myInnerFunction() {
          /* ... */
        }


/* ... */ }

    When interpreted, the function, myGlobalFunction, will be
    created immediately. However, the function, myInnerFunction,
    will not exist yet.

    When the outer function is called, the inner function will
    then be created before statements within myGlobalFunction are
    executed. When the scope of myGlobalFunction is left, the
    inner function will be destroyed.


Incidentally, variable declarations work much the same way. Declared variables within a particular execution context are created before any code is executed. If an initialisation is included with the declaration

      var myVariable = ...;

    the variable will exist from the start, but actual
    assignment will not occur until that point in the code is
    reached and evaluated.

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
.



Relevant Pages

  • Re: pseudo-namespacing in JavaScript
    ... SCRIPT element the outcome becomes very different. ... assignment, and if the DIV element had been shown to proceed the SCRIPT ... execution context where the spec says it should not. ... that function declarations will replace the ...
    (comp.lang.javascript)
  • Go ahead. Stop programming. This ensures you from any mistakes.
    ... declarations, ... All normal languages obey it ... right away from simplicity. ... of instruction execution. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: IE javascript bug: global variable
    ... Global variable declarations result in properties of the ... actual execution of code in that context. ... That's another aspect of this bug that puzzles me; ... both variable instantiation and actual code ...
    (comp.lang.javascript)
  • Re: are inner functions always properties?
    ... the difference lies in when the function objects would be created. ... function outer() { ... would work as expected because the nested function, inner, would have been created before execution of the function body had started. ...
    (comp.lang.javascript)
  • Re: Go ahead. Stop programming. This ensures you from any mistakes.
    ... Statements can be, and some definitely are, executed in parallell or out of sequence, as long as the result in every relevant aspect is the same as if they were executed in the sequence specified in the code. ... truth that the variables in the imerative languages, which allow deferred declarations, are visible only past declaration. ... In a switch statement you are jumping into one of many code blocks, it's not at all unreasonable that each block should end with a statement that specifies where the execution should continue. ... This requirement adds a bit of unnecessary code in a few cases, but a bit of unnecessary code is very often used to keep the language consistent and clear. ...
    (microsoft.public.dotnet.languages.csharp)

Loading