Monday, December 19, 2005

Dynamic scope

Now I'm confused by another aspect of JavaScript scope:
var f = function() { alert("default") }
function test(b) {
if (b) {
function f() { alert("shadowed") }
}
f()
}
Evaluate test(true), and you'll see "shadowed". Evaluate test(false), and you'll see "default". Apparently function declarations are another instance where JavaScript is not lexically scoped (in addition to this, arguments, and the behavior of references to unbound variables).

Update: Oh, I forgot to mention the with construct -- another instance of dynamic scope.

Update: Also note that this is not the same thing as the behavior of var. If we write this function instead:
var f = function() { alert("default") };
function test(b) {
if (b) {
var f = function() { alert("overridden") };
}
f()
}
Then with test(true) we still get "overridden", but with test(false) we get an error from trying to apply undefined as a function. In other words, in this example, we have normal lexical scope with hoisting.

Update: I think this settles it--it's pretty much what it looks like. In chapter 13 of ECMA-262, the dynamic semantics of a FunctionDeclaration is to create a new binding in the "variable object" (ECMA-262 terminology for an environment frame) upon evaluation of the declaration.

If you imagine an environment as a list of frames, then in a lexically scoped language, this is a statically computable list. In a language with dynamic scope, frames in the environment may be generated or mutated at runtime, so it's not statically computable. JavaScript has some of both, so there are some things you can know statically about a variable reference, and some things you can't.

Update: The saga continues... I realized that I couldn't actually derive the above example from the official ECMAScript grammar! I see that others have made this surprising discovery as well: you can't nest FunctionDeclarations beneath Statements. Furthermore, you can't begin an ExpressionStatement with a FunctionExpression. As a result, it's impossible to derive the above program.

However, in practice, JavaScript engines do appear to accept the program, and use the dynamic semantics from chapter 13 as I describe above. But for strict ECMAScript, I can't seem to produce an example. I thought I could still force the issue by placing the nested function at the top-level of the enclosing function body and conditionally returning. But I was thwarted by two more interesting phenomena. First of all, when a var declaration and a function declaration compete to bind the same identifier, the var always wins, apparently. So this always produces "default", regardless of the value of b:
function test(b) {
var f = function() { alert("default") };
if (!b) {
f();
return;
}
function f() { alert("overridden") }
f()
}
Second, nested function declarations appear to be hoisted to the top of the function body. So the following example always produces "overridden":
var f = function() { alert("default") };
function test3(b) {
if (!b) {
f();
return;
}
function f() { alert("overridden") }
f()
}
Curious. At any rate, the behavior of popular JavaScript engines (Firefox and Rhino) seem to be a generalization of ECMAScript (by allowing FunctionDeclarations to appear nested within Statements) that is consistent with the spec (by obeying the behavior specified by section 13).

Now I need to confirm these two more properties of the language with the spec.

18 comments:

Anonymous said...

Function declarations are always hoisted to the top. Variable declaration are hoisted to the top as well, but below function declarations. Variable assignments stay where they are.

The spec is quite specific about that. (page 37/38). F.e.: "If there is already a property of the variable object with the name of a declared variable, the value of the
property and its attributes are not changed. Semantically, this step must follow the creation of the
FormalParameterList and FunctionDeclaration properties. In particular, if a declared variable has the same name as a declared function or formal parameter, the variable declaration does not disturb the existing property."

Dave Herman said...

Why thank you! That was so much easier than combing the spec myself. :) Much obliged.

Anonymous said...

Nice information it was really informative....I was looking for digital course in Patna, found one today.

Anonymous said...

digital marketing course in Patna
Summer Industrial Training at patna
Winter Industrial Training at Patna

Anonymous said...

this is a very nice website. I read an article to know more information, that was superb. Digital Marketing Course in patna.

this is a very nice website. I read an article to know more information, that was superb. Digital Marketing Training in patna.

this is a very nice website. I read an article to know more information, that was superb. Digital Marketing Institute in patna.


Roksy said...

this is a very nicely website.
best mobile repairing course in patna
best mobile repairing in India
best institute of mobile repairing in India

Sunil Digital Brolly said...

Nice blog.

Social media marketing course from Digital Brolly helps you to make a career in this demanding and growing industry in these courses, learn how to spend and make strategy different business requirements and convert the leads to sales.

bhavanisai said...

nice blog,
Earn Money Online
Enroll in our Affiliate Marketing course in Hyderabad to learn how to earn money online by becoming an affiliate.

bhavanisai said...

nice blog,
Earn Money Online
Enroll in our Affiliate Marketing course in Hyderabad to learn how to earn money online by becoming an affiliate.

bhavanisai said...

nice blog,
Jobs in MNC’s
Only MNC & established companies are using DFP(doubleclick for publishers) at this point. so learn doubleclick for publishers to get hired in these companies.

Unknown said...

You done a good job as you Organized this article for digital marketer in community.

very nice article thanks healthcare at home in kanpur

Unknown said...

very nice article thanks medical equipment on rent
very nice article thanks icu at home kanpur

Anonymous said...

I’m Ravi Varma, a Serial Entrepreneur who enjoys exploring new ideas and business opportunities. I am a fan of technology, design, and entrepreneurship.

SEO Bureau said...

DutchSEOClark is het beste SEO Bureau van Nederland. Met een team van ervaren SEO specialisten helpen wij u graag de vindbaarheid van uw website Ranking te verhogen.

First DigiShala said...

Great Post, thank you for sharing this valuable and interesting information.
Visit us: First DigiShala

Sameer said...

Digital Marketing is trending job in market. Companies are seriously looking for Professional Digital Mareketers. Here are theBest Digital Marketing Institutes in Hyderabad

Digital Marketing Course in Rajouri Garden said...

Digital marketing course in Delhi are an ideal way to further your career. You can get advice from experienced marketing professionals and establish your professional network as soon as you can. In fact, Digital Marketing Institute Digital Marketing course recently introduced a unique certification course for business leaders. It is designed to help improve your knowledge of marketing and boost the confidence to achieve your goals. Whether you are a senior marketer or rising star, this program is ideal for you. The program includes 12 classes and online modules, the program also features interactive Q&As along with support from a community of experts. It will provide all the information you need to make it. Presented by Raja Rajamannar, a senior market leader this program is open to senior marketing professionals, in addition to to professionals across all functions.

Afreen Ansari said...

Digital Edge Institute offers the finest Digital Marketing Training in Noida, equipping learners with cutting-edge skills in SEO, SEM, social media marketing, and more. With expert faculty and hands-on experience, it's the premier destination for mastering digital strategies.
https://www.digitaledgeinstitute.com