/    Sign up×
Community /Pin to ProfileBookmark

I have some code like the following:

[CODE]{
var object = 1;
{
var object = 2;
}
alert(“object = “+object);
}[/CODE]

And when alert is executed it shows that object has the value 2. Shouldn’t scoping rules result in the alert reporting that object has the value 1, since the object defined within the inner brackets is not visible to the code within the outer bracket?

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@rnd_meJan 11.2010 — the brackets don't do anything; this is a javascript forum.
Copy linkTweet thisAlerts:
@Jeff_MottJan 11.2010 — Hi, RogerInHawaii. It's understandable why you'd assume braces create a new scope, because that's what many other C-style languages do. But JavaScript is different. Instead, JavaScript creates a new scope only within new functions. Although, JavaScript lets you nest functions within other functions, so that gives you something to toy with.
Copy linkTweet thisAlerts:
@RogerInHawaiiauthorJan 11.2010 — the brackets don't do anything; this is a javascript forum.[/QUOTE]

And that was a javascript question. It may be a naive question, coming from someone who is more familiar with other languages in which brackets actually mean something in regards to scoping, but it's a javascript question nonetheless.
Copy linkTweet thisAlerts:
@RogerInHawaiiauthorJan 11.2010 — JavaScript is different. Instead, JavaScript creates a new scope only within new functions. Although, JavaScript lets you nest functions within other functions, so that gives you something to toy with.[/QUOTE]

Wow. I read "JavaScript: The Definitive Guide" by David Flanagan cover to cover ( a riveting story! ) and don't recall coming across the scoping rules. And when I encountered an issue which seemed to point to just such a problem in my code I looked through it again, and did some searches on the internet, and nothing provided the "definitive" answer. So, thank you Jeff, once again.


I had assumed (yes, I know, assume nothing) that it was similar to C style scoping. I often find that it would be interesting to be able to talk to whomever came up with javsacript and ask how they came to certain decisions about the language. Why [I]not[/I] use the same scoping rules as C, for example?

And it might be nice, for example, when you do something like this:

[CODE]{
var MyVariable = 23;
{
var MyVariable = 92;
}
}[/CODE]


if it were to actually tell you, during the parsing of the code, that you've essentially double-defined a variable. But, no, it lets it go on its merry way, overwriting the prior variable.

Guess I better take a serious look through my code and see where else I might have made that invalid scoping assumption.
Copy linkTweet thisAlerts:
@Declan1991Jan 11.2010 — JavaScript uses [url=https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Working_with_Closures]Closures[/url], which makes it quite different to C. The similarity is only in syntax, very superficial.

And by the way, JavaScript is a dialect of [url=http://en.wikipedia.org/wiki/ECMAScript]ECMAScript[/url]. Internet Explorer uses another dialect, JScript.
×

Success!

Help @RogerInHawaii spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...