- <script type="text/javascript">
-
var a=b=c="abc"
-
document.write('#1. a=',a,', b=',b,', c=',c,'<br>')
-
function fabc() {
-
var a=b=c="fab";
-
document.write('#2. a=',a,', b=',b,', c=',c,'<br>')
-
}
-
fabc();
-
document.write('#3. a=',a,', b=',b,', c=',c,'<br>')
-
</script>
<!--
Actual Output(s):
#1. a=abc, b=abc, c=abc
#2. a=fab, b=fab, c=fab
#3. a=abc, b=fab, c=fab
Expected: Output #3 must be the same as Ouput #1
Actual result: variable(s) 'b' and 'c' overwritten inside function 'fabc()'.
variables 'b' and 'c' defined as Local but act as Global variables!
Thanks
-->
6 1353
Heya, Vladimir. Welcome to TSDN!
For your protection, I have removed your email address from your post. Please read the Posting Guidelines.
You'll want to have a look at this article for an explanation of why you're not getting what you're expecting.
Heya, Vladimir. Welcome to TSDN!
For your protection, I have removed your email address from your post. Please read the Posting Guidelines.
You'll want to have a look at this article for an explanation of why you're not getting what you're expecting.
Look. -
function fabc() {
-
var a=b=c="fab";
-
document.write('#2. a=',a,', b=',b,', c=',c,'<br>')
-
}
-
//Here b and c is not local to fabc, that's why it happens so.
-
//These r global.
-
//Only a is local to this function.
-
//If you do like .... var a,b,c;a=b=c=fab; Only then your output would be as u //expected
-
Kind regrdas,
Dmjpro.
Hello guys, thanks for replay to my Q `JavaScript Error in Multiple declarations`
I read on article provided by `pbmods ` and `dmjpro` and ... didn't fined anything about `Multiple declarations`.
Yes, I know what happend, I just can't understand why in-the-same-declaration
some variable(s) get status `Global` and some `Local`. As much I know this `Multiple declarations` is very `leagal` in JavaScript.
Do we have some Official documentation about this?!
Thanks again. VladimirOrlovsky. 2007Aug22wed14:22
Heya, Vladimir.
When you do this:
Working from right to left, you assign the variable c a value of 'fab'. Then you assign the variable b to the value of c. Then you create a local variable named a and assign it the value of b.
Note the distinction; only a is a local variable here.
As DMJPro mentioned, if you want to make all three variables local, you need to use the comma operator: -
var a, b, c;
-
a = b = c = 'fab';
-
That's about as close to what you're trying to accomplish as you're going to get.
Thanks `pbmods` .
Now, I get it.
weird but make sense.
Vladimir Orlovsky
Heya, Vladimir.
As a general rule of thumb, it's a better idea to declare one variable per line anyway, as that gives you ample space to comment its purpose.
About the only time I can think of that you would want to declare multiple variables on the same line would be in the context of a let statement: -
let(x = 5, y = 10, z = ' plus a billion')
-
{
-
alert(x * y + z); // 50 plus a billion
-
}
-
-
alert(x); // undefined
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Steven T. Hatton |
last post by:
Some people have suggested the desire for code completion and refined
edit-time error detection are an indication of incompetence on the part of
the programmer who wants such features. ...
|
by: Java script Dude |
last post by:
I have still yet to see a JavaScript Editor that comes close to
reading a good JS book, learing it and using it with a text editor.
Anyway, here my recipe for build successfull DHTML...
|
by: Peter Frost |
last post by:
Please help
I don't know if this is possible but what I would really like to do is
to use On Error Goto to capture the code that is being executed when
an error occurs.
Any help would be much...
|
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...
|
by: andrewfsears |
last post by:
I have a question: I was wondering if it is possible to simulate the
multiple constructors, like in Java (yes, I know that the languages are
completely different)?
Let's say that I have a class...
|
by: Raman |
last post by:
Hi All,
I have two libs (libFirst.a and libSecond.a). Both libs contains a
common function func(). Now I want to link an application "app" with
these two libs as
gcc -o app libFirst.a...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |