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?
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() { }
A must read about the types of function definitions in JavaScript.
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.