473,396 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Closures bug?!

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

Sep 12 '07 #1
1 1270
Na***********@gmail.com wrote:
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.

Sep 13 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Alexander May | last post by:
When I define a function in the body of a loop, why doesn't the function "close" on the loop vairable? See example below. Thanks, Alex C:\Documents and Settings\Alexander May>python Python...
5
by: paolo veronelli | last post by:
I've a vague idea of the differences,I don't know scheme anyway. I'd like to see an example to show what is missing in python about closures and possibly understand if ruby is better in this...
4
by: Marc Tanner | last post by:
Hello, I am currently working on a eventhandling system or something similar, and have the problem of loosing scope. I have read many interesting posts on this group and the faq article about...
2
by: Jake Barnes | last post by:
Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is...
16
by: Karl Kofnarson | last post by:
Hi, while writing my last program I came upon the problem of accessing a common local variable by a bunch of functions. I wanted to have a function which would, depending on some argument,...
4
by: king kikapu | last post by:
Hi, i am trying, to no avail yet, to take a C#'s overloaded functions skeleton and rewrite it in Python by using closures. I read somewhere on the net (http://dirtsimple.org/2004/12/python-is-...
2
by: Jon Harrop | last post by:
Just debating somewhere else whether or not Python might be considered a functional programming language. Lua, Ruby and Perl all seem to provide first class lexical closures. What is the current...
26
by: Aaron \Castironpi\ Brady | last post by:
Hello all, To me, this is a somewhat unintuitive behavior. I want to discuss the parts of it I don't understand. .... f= lambda: n .... 9 9
4
by: MartinRinehart | last post by:
I've written a short article explaining closures in JavaScript. It's at: http://www.martinrinehart.com/articles/javascript-closures.html I think I've understood. I look forward to your...
40
by: MartinRinehart | last post by:
I've rewritten a short article explaining closures in JavaScript. It's at: http://www.martinrinehart.com/articles/javascript-closures.html A big Thank You to PointedEars and Jorge for helping...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.