Consider the following JavaScript program var foo function
. Consider the following JavaScript program: var foo - function(x) i var bar = function() { return function(x) { return f(f(x)); }; }; return bar (function(y) f return y +x; a. what is the value of foo (3) (2) according to the standard (statically scoped) semantics of JavaScript? Now, suppose we try to optimize the above function by first inlining the bar function var foo = function(x) return function (x) return (function(y) ( return y + x; >) ((function(y) return y + beta reducing var foo = function(x) { return function (x return (function (y)return y + x; (x +x); and beta reducing again var foo = function(x) { return function(x) ( return x+ x + x, i
Solution
a.
the output is 8.
b.
foo(3)(2) would be 6. inner function x will be 2. so 2+2+2 will be 6.
c.
In the optimization we are leaving the outer variable x.
d.
x inside foo:3
x inside bar:2
y insde f : 2,x inside f:3
y insde f:5,x inside f:3
5+3 = 8.
