Closures bug?!

N

Nathan.M.Snow

Why does f() === 'undefined' at the end of the following code snippet?

var f = 3;
f = function(){var f = f; return function(){return f};};
alert(f());

My understanding of closures is that the base environment of a closure
is the same as when it was created. When invoked, the entire
environment is the parameter bindings unioned with this base
environment, and the parameter bindings would shadow the base
environment.

Therefore, the environment of the outer function is the global
environment and the environment of the inner is the environment
created by the outer.

Furthermore, each of the following examples return 3 as one would
expect.

var f = 3;
f = function(){var x = f; return function(){return x};}();
alert(f());

var f = 3;
f = function(f){return function(){return f};}(f);
alert(f());

Thanks!

Sincerely,

Nathan
 
R

Richard Cornford

Why does f() === 'undefined' at the end of the following code snippet?

var f = 3;
f = function(){var f = f; return function(){return f};};
alert(f());

No it does not, it alerts "function(){return f}", but I know what you
are getting at.

You mean either:-

alert(f()());

or:-

f = (function(){var f = f; return function(){return f};})();
My understanding of closures is that the base environment of
a closure is the same as when it was created. When invoked,
the entire environment is the parameter bindings unioned with
this base environment, and the parameter bindings would shadow
the base environment.

Therefore, the environment of the outer function is the global
environment and the environment of the inner is the environment
created by the outer.

Yes, but variable instantiation happens prior to the execution of any
function body code so when you do:-

var f = f;

- in the first inner function an - f - property had been created on the
Variable object for that execution context, with its value defaulted
to - undefined -, during variable instantiation and so when the
assignment is eventually evaluated and the right hand side of the
assignment resolves the identifier - f - it finds that that Variable
object has the property and returns its value, the default undefined
value, and assigns that value to the - f - property of the same Variable
object. So when you return - f - you get the undefined value you
assigned.

The existence of a global - f - is unimportant after the first
function's variable instantiation as that value has then been shadowed.
Furthermore, each of the following examples return 3 as one would
expect.

var f = 3;
f = function(){var x = f; return function(){return x};}();
alert(f());

var f = 3;
f = function(f){return function(){return f};}(f);
alert(f());

Yes, they would.

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top