1

I am going through Head First JavaScript to understand nested functions. Below is the code I am failing to understand.

var migrating = true;
if (migrating) {
    quack(4);
    fly(4);
}

var fly = function(num) {
    var sound = "Flying";
    for (var i = 0; i < num; i++) {
        wingFlapper();
    }
    function wingFlapper() {
       console.log(sound);
    }
};

function quack(num) {
    var sound = "Quack";
    for (var i = 0; i < num; i++) {
        quacker();
    }
    var quacker = function() {
        console.log(sound);
    };
}

The book says "quacker is defined by a function expression in the function quack. So its scope is the entire quack function but it’s defined only after the function expression is evaluated, until the end of the function body." So I am calling the function before its evaluated so shouldn't it throw an exception or I am not understanding the concept?

Michael Geary
  • 28,450
  • 9
  • 65
  • 75
  • I'm getting a `Uncaught TypeError: quacker is not a function` in chromes console. Take a look at this [fiddle](http://jsfiddle.net/34h4e2dd/), any messages in the console when you run? – Spencer Wieczorek Jul 12 '15 at 18:51
  • I'm failing to understand it because it's badly indented. http://jsbeautifier.org/ – Bergi Jul 12 '15 at 19:17
  • 1
    I cleaned up the indentation for you. It's vital to use correct indentation to indicate the structure of your code. (@Bergi, looks like my edit crossed paths with your comment.) – Michael Geary Jul 12 '15 at 19:21
  • 1
    I also took the liberty of moving the `{` characters to the previous lines instead of having them on a line by themselves. In many "curly brace" languages (C, Java, etc.) it doesn't matter which style you use, but in JavaScript it's a "best practice" to put the `{` at the end of the previous line because of automatic semicolon insertion. In many cases it won't matter, but there is at least one situation where the `{` character on a separate line won't work (returning an object literal). So for consistency it's best to follow the above style throughout. – Michael Geary Jul 12 '15 at 19:21

2 Answers2

0

This should error out. quacker() is a function expression which follows top down approach. If you change that function expression to function statement it should work fine. I know it is confusing have a look at this

Why can I use a function before it's defined in Javascript?

Try this. It should not error out as we are using function statement which gets bound to the context (global here) before any thing else executes.

function qaucker() 
{
     console.log(sound);
};
Community
  • 1
  • 1
-1

That's right. It should. If you try the example in some browser console you will see something like this:

Uncaught TypeError: quacker is not a function
    at quack (<anonymous>:26:1)
    at <anonymous>:5:1
    at Object.InjectedScript._evaluateOn (<anonymous>:895:140)
    at Object.InjectedScript._evaluateAndWrap (<anonymous>:828:34)
    at Object.InjectedScript.evaluate (<anonymous>:694:21)
Mariy
  • 5,746
  • 4
  • 40
  • 57