473,770 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Weird variable scope

Over the weekend I attended a session on JavaScript at the No Fluff
Just Stuff conference and learned an interesting quirk that I wanted to
ask a question about..

Take this code:

x = 5;

function foo() {
alert(x);
var x = 10;
alert(x);
}

foo();

The first alert will display 'undefined', and the second will display
10. Apparently JavaScript recognizes the 'var x = 10' declaration
inside foo() even before that line is reached - it spots the
declaration, but not the initialization to 10. Only after x is set to
10 and the second alert is reached will the 10 be displayed.

My question is... is there any way from this code to access the global
x variable (5) from within foo()?

Mar 15 '06
13 1436
Thomas 'PointedEars' Lahn wrote:
And in a browser context (which is assumed on this group) that is
always true.

No, it is not.


Name a browser where it is not true.

Note: I didn't say it is _required_ to be true. I said it _is_ always true.
And will continue to be.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Mar 17 '06 #11
Matt Kruse wrote:
Thomas 'PointedEars' Lahn wrote:
And in a browser context (which is assumed on this group) that is
always true. No, it is not.


Name a browser where it is not true.


For example, any browser showing an SVG document using Adobe SVG Viewer
(ASV). Tell me, how many times do you need to be told before you will
accept that? Twenty? A hundred?
Note: I didn't say it is _required_ to be true.
Which would have been only equally false.
I said it _is_ always true.
But it is not.
And will continue to be.


Therefore, it will not.
PointedEars
Mar 17 '06 #12
Thomas 'PointedEars' Lahn <Po*********@we b.de> writes:
For example, any browser showing an SVG document using Adobe SVG Viewer
(ASV).


The context of that is the ASV, not a browser. That the ASV happens
to run as a plugin in a browser does not change that.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Mar 17 '06 #13
Thomas 'PointedEars' Lahn said the following on 3/17/2006 5:46 PM:
Matt Kruse wrote:
Thomas 'PointedEars' Lahn wrote:
And in a browser context (which is assumed on this group) that is
always true.
No, it is not.

Name a browser where it is not true.


For example, any browser showing an SVG document using Adobe SVG Viewer
(ASV). Tell me, how many times do you need to be told before you will
accept that? Twenty? A hundred?


That's the plugin you are referencing, not the browser. Now, how many
times do you need that pointed out to *you* before you accept it?
Twenty? A hundred?

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 18 '06 #14

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

Similar topics

11
1703
by: Afzal Mazhar | last post by:
What is wrong with this? class Example1 { public static long myClassVar; public static void Main() { { long myClassVar = 5;
23
19203
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
22
2064
by: Daniel Rucareanu | last post by:
I have the following script: function Test(){} Test.F = function(){} Test.F.FF = function(){} Test.F.FF.FFF = function(){} Test.F.FF.FFF.FFFF = function(){} //var alias = function(){}; var alias = Test.F.FF.FFF.FFFF;
12
1762
by: sparks | last post by:
My boss and I both have access 97 and access 2003 installed on our systems. Lately he has been having a lot of problems. Databases that won't close. The database looks like it closes but access will not. And other weird things...I just passed it off as just MS nothing new. Yesterday I asked him to look at a little project...nothing much of anything. I put it up on our server and he opened it and went into design view. Its a form with...
2
2355
by: vlsidesign | last post by:
Here is my a portion of my program: #include <stdio.h> main() { int fahr, celsius; int lower, upper, step; int fc_conv(int fahr); ... snip ... }
1
25699
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause bad breath)? Scope describes the context in which a variable can be used. For example, if a variable's scope is a certain function, then that variable can only be used in that function. If you were to try to access that variable anywhere else in...
2
1528
by: Matt Nordhoff | last post by:
Sebastjan Trepca wrote: Python doesn't like when you read a variable that exists in an outer scope, then try to assign to it in this scope. (When you do "a = b", "b" is processed first. In this case, Python doesn't find a "value" variable in this scope, so it checks the outer scope, and does find it. But then when it gets to the "a = " part... well, I don't know, but it doesn't like it.)
112
5475
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions that may print some messages. foo(...) { if (!silent)
11
1389
by: ssecorp | last post by:
I am never redefining the or reassigning the list when using validate but since it spits the modified list back out that somehow means that the modified list is part of the environment and not the old one. i thought what happend inside a function stays inside a function meaning what comes out is independent of what comes in. Meaning if I want the list I send as a parameter to the function I have to do x = func(x) and not just func(x) and x...
0
9595
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9432
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10232
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8891
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3974
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.