Given the following code:

var b = 6;

var foo = function (a) {
    a = b + a;
    return function () { return a; };
};

b = 2;

var bar = function () {
    var b = 3;
    return foo(b);
};

what would bar()() evaluate to, if JavaScript were dynamically scoped ?

6
  • 9
  • 5
  • a function
  • error

Determine what the foo and bar functions return.

Keep in mind the difference between a function definition expression and a function call.

Note that the code defines two variables called b.

Remember the difference between the static (or compile-time) context and the dynamic (or run-time) context.