alert(foo); ...">

Javascript parse / order order?

This is probably a question, but I do not understand why this works:

<script type="text/javascript"> alert(foo); function foo() { } </script> 

This function "warns" foo () {} ", but I expected the warning to be evaluated before the foo function is defined. Can anyone explain what I don't understand about the parsing or evaluation order, or indicate me to a resource that does?

+2
javascript
source share
3 answers

JavaScript, like PHP, tracks top-level function declarations until the code runs. However, you can bypass the auto-function with the following assignments:

var a = function a() { }

+3
source share

A must read about the types of function definitions in JavaScript.

Demystified Named Function Expressions

0
source share

Function declarations go up and therefore are declared first.

You can change this behavior by assigning them such a variable.

 var a = function() { // do it }; 

This assigns a variable an anonymous function.

0
source share

All Articles