Something to note in regard to function declarations is they are "hoisted" before the script executes. This means function declarations (and 'var' declarations) are loaded to memory at compile time . Therefore, functions can be declared at any point in a script with the assurance they will be available at runtime wherever they are called. Nonetheless, it is good practice to group declarations at the top of the applicable scope.
One particularly interesting and useful paradigm supplied by function expressions is the ability execute them immediately. This is rightly called an "Immediately Invoked Function Expression" or "IIFE". To invoke a function expression right away, simply append '();' after the closing '}', with the necessary arguments (if any) provided within the parenthesis. Though if the expression is the only item in the statement, then an additional set of parentheses wrapping the entire expression (except the colon) will be needed. These constructs are quite useful for non-trivial initialization code since they keep a cleaner namespace by preventing scope leakage.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.