473,387 Members | 1,721 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,387 software developers and data experts.

Please confirm my suspicions on variable ordering

With teh following code, if I put picnames first, it wont work because
the other variables havent yet been defined, correct? That seems to be
the case. What I want to know is whether that is a feature or a bug.


var r1 = new Array(
"Anna",
"Jane",
"Hannah",
"Nayomi"
);
var r2 = new Array(
"My name is ",
"I'm "
);
//
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~
var d1 = r1[Math.floor(Math.random() * 4)];
var d2 = r2[Math.floor(Math.random() * 2)];
//
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~
var picnames = new Array(
"Good morning.",
"Good morning.",
"What's your name?",
d2 + d1 + ".",
"Thank you.",
"You're welcome."
);


--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #1
2 1360
Lee
Fabian said:

With teh following code, if I put picnames first, it wont work because
the other variables havent yet been defined, correct? That seems to be
the case. What I want to know is whether that is a feature or a bug. var picnames = new Array(
"Good morning.",
"Good morning.",
"What's your name?",
d2 + d1 + ".",
"Thank you.",
"You're welcome."
);


It's a very important feature. When picnames is created,
the value of the fourth element is determined by evaluating
the current value of d1 and d2. If the values of d1 and d2
change, the value of the fourth element won't change.
That's the way you'll find that you almost always want it to
work.

If you want the fourth element of picnames to change when
you change d1 and d2, just change it at the same time.

Jul 20 '05 #2
JRS: In article <bn************@ID-174912.news.uni-berlin.de>, seen in
news:comp.lang.javascript, Fabian <la****@hotmail.com> posted at Fri, 24
Oct 2003 07:31:13 :-
With teh following code, if I put picnames first, it wont work because
the other variables havent yet been defined, correct? That seems to be
the case. What I want to know is whether that is a feature or a bug.


It is neither; it is a necessity.

The interpreter reads the code in order, executing what it finds; and
must already know about any variables on the right of an assignment.
After page load, functions may be called, and again all values needed
must be pre-defined, in terms of the chronological sequence of
execution.

Note : function X(Y) { Z } is a definition; the function is not
thereby executed, and the RHS may contain unknowns. In effect, the
function text is just remembered.

That's simplified.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #3

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

Similar topics

3
by: KathyB | last post by:
Hi, I've tried everything I can think of. Using the script below, I thought on submit my ConfirmSave function would run, then if true returned, would run my form action. At this point it appears...
0
by: Brian Reaburn | last post by:
Hello All: I have been struggling to find out what could possibly be wrong with this piece of code. From my understanding of the CONTAINSTABLE Function and using parameters with a query, this...
1
by: Muhammad Abdullah | last post by:
Hi am having some problems with the javascript confirm. i have it working fine on one page and it doesnt even pop up at the other. The code on the working page is, private void...
7
by: Tigger | last post by:
Dear Experts, I am working on ASP.NET. I have got a problem related to the usage of Javascript in ASP.NET. Please help. The story is the following: 1) I am developing an ASP.NET application. I...
8
by: Dave | last post by:
Hello all, I have been able to use a Javascript alert box in my vb.net web application, for example: Dim msg As String = "Hello, world." Dim alertScript As String = "<script...
4
by: Warren Sirota | last post by:
Hi, Please let me know if I am interpreting this correctly. I've done a little testing of the difference between passing parameters byVal and byRef, and the results were slightly non-intuitive,...
20
by: Aaron Graham | last post by:
/** * Sample usage: * unsigned long longvar = 0x12345678; * unsigned long be_longvar = endian::host_to_big(longvar); * unsigned short shortvar = 0x1234; * unsigned short le_shortvar =...
7
by: shil | last post by:
Hi all, Can any one direct me in how to use javascript confirm() function when a condition is met from codebehind. I don't want to attach this to a button. When I submit the page, I want to...
5
by: keeps21 | last post by:
Ok, so here goes. I need to retrieve data, (ie. id, title, introtext.) from my database and store each in a variable. I then need to repeat this for the next 4 records. So I would have 5...
4
by: mattehz | last post by:
Hey there, I am trying to upload old source files and came across these errors: Warning: Invalid argument supplied for foreach() in /home/mattehz/public_html/acssr/trunk/inc_html.php on line 59...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.