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).var f = function() { alert("default") }
function test(b) {
if (b) {
function f() { alert("shadowed") }
}
f()
}
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:
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.var f = function() { alert("default") };
function test(b) {
if (b) {
var f = function() { alert("overridden") };
}
f()
}
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:
Second, nested function declarations appear to be hoisted to the top of the function body. So the following example always produces "overridden":function test(b) {
var f = function() { alert("default") };
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).var f = function() { alert("default") };
function test3(b) {
if (!b) {
f();
return;
}
function f() { alert("overridden") }
f()
}
Now I need to confirm these two more properties of the language with the spec.
18 comments:
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."
Why thank you! That was so much easier than combing the spec myself. :) Much obliged.
Nice information it was really informative....I was looking for digital course in Patna, found one today.
digital marketing course in Patna
Summer Industrial Training at patna
Winter Industrial Training at Patna
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.
this is a very nicely website.
best mobile repairing course in patna
best mobile repairing in India
best institute of mobile repairing in India
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.
nice blog,
Earn Money Online
Enroll in our Affiliate Marketing course in Hyderabad to learn how to earn money online by becoming an affiliate.
nice blog,
Earn Money Online
Enroll in our Affiliate Marketing course in Hyderabad to learn how to earn money online by becoming an affiliate.
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.
You done a good job as you Organized this article for digital marketer in community.
very nice article thanks healthcare at home in kanpur
very nice article thanks medical equipment on rent
very nice article thanks icu at home kanpur
I’m Ravi Varma, a Serial Entrepreneur who enjoys exploring new ideas and business opportunities. I am a fan of technology, design, and entrepreneurship.
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.
Great Post, thank you for sharing this valuable and interesting information.
Visit us: First DigiShala
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 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.
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
Post a Comment