473,320 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

JavaScript Error in Multiple declarations

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. var a=b=c="abc"
  3. document.write('#1. a=',a,', b=',b,', c=',c,'<br>')
  4. function fabc() {
  5. var a=b=c="fab";
  6. document.write('#2. a=',a,', b=',b,', c=',c,'<br>')
  7. }
  8. fabc();
  9. document.write('#3. a=',a,', b=',b,', c=',c,'<br>')
  10. </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
-->
Aug 22 '07 #1
6 1353
pbmods
5,821 Expert 4TB
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.
Aug 22 '07 #2
dmjpro
2,476 2GB
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.

Expand|Select|Wrap|Line Numbers
  1. function fabc() {
  2. var a=b=c="fab";
  3. document.write('#2. a=',a,', b=',b,', c=',c,'<br>')
  4. }
  5. //Here b and c is not local to fabc, that's why it happens so.
  6. //These r global.
  7. //Only a is local to this function.
  8. //If you do like .... var a,b,c;a=b=c=fab; Only then your output would be as u //expected
  9.  
Kind regrdas,
Dmjpro.
Aug 22 '07 #3
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
Aug 22 '07 #4
pbmods
5,821 Expert 4TB
Heya, Vladimir.

When you do this:
Expand|Select|Wrap|Line Numbers
  1. var a = b = c = 'fab';
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:
Expand|Select|Wrap|Line Numbers
  1. var a, b, c;
  2. a = b = c = 'fab';
  3.  
That's about as close to what you're trying to accomplish as you're going to get.
Aug 22 '07 #5
Thanks `pbmods` .
Now, I get it.
weird but make sense.
Vladimir Orlovsky
Aug 23 '07 #6
pbmods
5,821 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. let(x = 5, y = 10, z = ' plus a billion')
  2. {
  3.      alert(x * y + z);  // 50 plus a billion
  4. }
  5.  
  6. alert(x);  // undefined
  7.  
Aug 23 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

67
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. ...
3
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...
6
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...
2
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...
7
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...
1
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...
0
isladogs
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...
1
isladogs
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...
0
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...
1
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)...
1
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
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
0
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...
0
isladogs
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...

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.