Can JavaScript use a function before it's defined?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jonathan Fine

    Can JavaScript use a function before it's defined?

    As subject. By 'before' I mean before in the file, not in time.

    Here's a file
    ===
    $ cat wierd.js
    f()
    function f(){print('hi') }
    f()
    function f(){print('ho') }
    f()
    ===

    And when I run SpiderMonkey on it
    $ js wierd.js
    I get - well, what do you think I get?

    OK. So I get
    ===
    $ js wierd.js
    ho
    ho
    ho
    ===

    So it seems that SpiderMonkey is picking up the LAST definition of f()
    and executing it three times. Not at all what I expected.

    I didn't find any relevant information in
    Generally speaking, a function is a "subprogram" that can be called by code external (or internal, in the case of recursion) to the function. Like the program itself, a function is composed of a sequence of statements called the function body. Values can be passed to a function as parameters, and the function will return a value.


    I'd like to read about this. Any pointers? In particular, does anyone
    know of a JavaScript book that documents this behaviour? And is it
    reliably cross-platform?

    --
    Jonathan
  • Jonathan Fine

    #2
    Re: Can JavaScript use a function before it's defined?

    VK wrote:
    Yes, this behavior is by design and cross-browser. Do not forget that
    function declarations are not a part of the program execution flow.
    Thanks for this. I guess there may be a difference between function
    declaration and function definition. For example
    f = function f(){print('hi') }
    is not always equivalent to
    function f(){print('hi') }

    I was expecting the latter to have the actual meaning of the former!
    I am not ready to give an exact reference. I assume you can find
    relevant text in ECMA-262 3rd ed. in the description of parser and
    tokenizer algorithms.
    [explanation snipped]

    I'd really appreciate a reference that gives all these not-so-obscure
    gotchas in JavaScript language, so that I know where to look the next
    time I come across a gotcha like this.
    With a description of your actual intent a regular coding solution
    could be suggested.
    I wanted to understand why SpiderMonkey was not doing what I expected.

    I wrote:

    // Dummy definition
    if (typeof fn == 'undefined') function fn(){}

    // Lots of lines like ...
    fn(123)

    // Real definition of fn
    function fn(){
    print(arguments )
    //...
    }

    I thought this would allow me to structure the code in the way I wanted
    to, and was surprised that the dummy def'n of fn was never used.

    Thank you for your help in understanding some of the intricacies of
    JavaScript.

    --
    Jonathan

    Comment

    • RobG

      #3
      Re: Can JavaScript use a function before it's defined?

      On Jun 21, 9:19 am, Jonathan Fine <jf...@pytex.or gwrote:
      VK wrote:
      Yes, this behavior is by design and cross-browser. Do not forget that
      function declarations are not a part of the program execution flow.
      >
      Thanks for this. I guess there may be a difference between function
      declaration and function definition. For example
      f = function f(){print('hi') }
      is not always equivalent to
      function f(){print('hi') }
      >
      I was expecting the latter to have the actual meaning of the former!
      They are essentially equivalent, the difference is that function (and
      variable) declarations are processed before any code is executed.
      Function expressions, e.g.

      var foo = function() {}

      are evaluated when the code is run. In the above, when execution
      begins, foo has a value of undefined until the line assigning it the
      anonymous function is executed.

      >
      I am not ready to give an exact reference. I assume you can find
      relevant text in ECMA-262 3rd ed. in the description of parser and
      tokenizer algorithms.
      >
      [explanation snipped]
      >
      I'd really appreciate a reference that gives all these not-so-obscure
      gotchas in JavaScript language, so that I know where to look the next
      time I come across a gotcha like this.
      It's not a "gotcha", it is a well-known feature of the language and is
      not unique to ECMAScript. Try section 10 Execution Contexts and more
      specifically, section 10.1.3 Variable Instantiation.

      >
      With a description of your actual intent a regular coding solution
      could be suggested.
      >
      I wanted to understand why SpiderMonkey was not doing what I expected.
      >
      I wrote:
      >
      // Dummy definition
      if (typeof fn == 'undefined') function fn(){}
      >
      // Lots of lines like ...
      fn(123)
      >
      // Real definition of fn
      function fn(){
      print(arguments )
      //...
      }
      >
      I thought this would allow me to structure the code in the way I wanted
      to, and was surprised that the dummy def'n of fn was never used.
      Because before the code was executed, the function declaration had
      been processed as a component of variable instantiation and fn was a
      function object.

      To get the effect you expected:

      var fn;

      // Dummy definition
      if (typeof fn == 'undefined') function fn(){}

      // Lots of lines like ...
      fn(123)

      // Real definition of fn
      fn = function (){
      print(arguments )
      //...
      }



      --
      Rob

      Comment

      • Joost Diepenmaat

        #4
        Re: Can JavaScript use a function before it's defined?

        Jonathan Fine <jfine@pytex.or gwrites:
        VK wrote:
        >
        >Yes, this behavior is by design and cross-browser. Do not forget that
        >function declarations are not a part of the program execution flow.
        >
        Thanks for this. I guess there may be a difference between function
        declaration and function definition. For example
        f = function f(){print('hi') }
        is not always equivalent to
        function f(){print('hi') }
        >
        I was expecting the latter to have the actual meaning of the former!
        Well, javascript is kind of a hack - but then, many popular languages
        are. In any case, as a rule of thumb, "function name() {}" is
        evaluated at the earliest possible time, while "name = function(){}"
        is evaluated at the latest possible time.

        All of the above is a convenient lie.

        --
        Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

        Comment

        Working...