Array - What am I doing wrong?

M

M.Siler

<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT>
<!--

var factor_val = new Array(8,7)

factor_val[1,1] = 68.8
factor_val[1,2] = 55
factor_val[1,3] = 45.5
factor_val[1,4] = 42.3
factor_val[1,5] = 39.3
factor_val[1,6] = 34.4
factor_val[1,7] = 30.6
factor_val[2,1] = 36.7
factor_val[2,2] = 31
factor_val[2,3] = 25.8
factor_val[2,4] = 23.8
factor_val[2,5] = 22.1
factor_val[2,6] = 19.4
factor_val[2,7] = 17.2
factor_val[3,1] = 30.6
factor_val[3,2] = 26.5
factor_val[3,3] = 20.4
factor_val[3,4] = 18.8
factor_val[3,5] = 17.5
factor_val[3,6] = 15.3
factor_val[3,7] = 13.6
factor_val[4,1] = 24.3
factor_val[4,2] = 19.7
factor_val[4,3] = 16.5
factor_val[4,4] = 15.2
factor_val[4,5] = 14.1
factor_val[4,6] = 12.4
factor_val[4,7] = 11
factor_val[5,1] = 17.2
factor_val[5,2] = 13.7
factor_val[5,3] = 11.5
factor_val[5,4] = 10.6
factor_val[5,5] = 9.8
factor_val[5,6] = 8.6
factor_val[5,7] = 7.6
factor_val[6,1] = 9.7
factor_val[6,2] = 7.7
factor_val[6,3] = 6.5
factor_val[6,4] = 5.9
factor_val[6,5] = 5.5
factor_val[6,6] = 4.8
factor_val[6,7] = 4.3
factor_val[7,1] = 7.6
factor_val[7,2] = 6
factor_val[7,3] = 5.1
factor_val[7,4] = 4.7
factor_val[7,5] = 4.4
factor_val[7,6] = 3.8
factor_val[7,7] = 3.4
factor_val[8,1] = 4
factor_val[8,2] = 3
factor_val[8,3] = 2.7
factor_val[8,4] = 2.6
factor_val[8,5] = 2.5
factor_val[8,6] = 2
factor_val[8,7] = 1.7

function compute(form)
{
var myindex_x = form.diameter.selectedIndex;
var myindex_y = form.depth.selectedIndex;

if (myindex_x == 0)
{
alert("Select a Diameter.");
}
else if (myindex_y == 0)
{
alert("Select a Depth.");
}
else
{
form.x_val.value = myindex_x;
form.y_val.value = myindex_y;
form.f_val.value = factor_val[myindex_x, myindex_y];
form.answer_val.value = form.num_of_holes.value/factor_val[myindex_x,
myindex_y];

}
}

-->
</SCRIPT>
</HEAD>

<BODY>

<form>

<p><input type="text" name="num_of_holes" size="4" value="0"> Number of
Holes</p>

<p>
<select name="diameter">
<option>Select the Diameter</option>
<option>6</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>12</option>
<option>16</option>
<option>18</option>
<option>24</option>
</select>
</p>

<p>
<select name="depth">
<option>Select the Depth</option>
<option>24</option>
<option>30</option>
<option>36</option>
<option>39</option>
<option>42</option>
<option>48</option>
<option>54</option>
</select>
</p>

<p><font color="#0000FF"><input type="text" name="x_val" size="4" value="0"
onFocus='this.blur()'> X</font></p>
<p><font color="#0000FF"><input type="text" name="y_val" size="4" value="0"
onFocus='this.blur()'> Y</font></p>
<p><font color="#0000FF"><input type="text" name="f_val" size="4" value="0"
onFocus='this.blur()'> Factor</font></p>
<p><input type="text" name="answer_val" size="6" value="0"
onFocus='this.blur()'> Answer</p>

</p>
<INPUT TYPE="BUTTON" VALUE="Calculate" onClick="compute(this.form)"><input
type="reset" value="Reset" name="B2">
</form>
</BODY>
</HTML>
 
E

Evertjan.

M.Siler wrote on 16 nov 2003 in comp.lang.javascript:
var factor_val = new Array(8,7)

There are no cats in America,
I mean there are no multidimensional arrays in Javascript.


<script>

var factor_val = new Array(8,7)

alert(factor_val[0]) // shows 8
alert(factor_val[1]) // shows 7

</script>
 
J

Juliette

Evertjan. said:
M.Siler wrote on 16 nov 2003 in comp.lang.javascript:
var factor_val = new Array(8,7)

There are no cats in America,
I mean there are no multidimensional arrays in Javascript.

<script>

var factor_val = new Array(8,7)

alert(factor_val[0]) // shows 8
alert(factor_val[1]) // shows 7

</script>


I don't agree and the NS javascript specs agree with me: there are
multidimensional arrays in Javascript, except you have to define them
slightly differently from what has been done in the sample code.

The following comes straight from the Client-Side Javascript Reference
(v 1.3):

** START QUOTE **

Example 2: Two-dimensional array. The following code creates a
two-dimensional array and assigns the results to myVar.

myVar="Multidimensional array test; "
a = new Array(4)
for (i=0; i < 4; i++) {
a = new Array(4)
for (j=0; j < 4; j++) {
a[j] = "["+i+","+j+"]"
}
}
for (i=0; i < 4; i++) {
str = "Row "+i+":"
for (j=0; j < 4; j++) {
str += a[j]
}
myVar += str +"; "
}

This example assigns the following string to myVar (line breaks are used
here for readability):

Multidimensional array test;
Row 0:[0,0][0,1][0,2][0,3];
Row 1:[1,0][1,1][1,2][1,3];
Row 2:[2,0][2,1][2,2][2,3];
Row 3:[3,0][3,1][3,2][3,3];

** END OF QUOTE **


So to answer Mr Siler's question:
You have to define your array using the following method:
** Please take note: normal array assignments start at 0 (not at 1), so
the first argument should be refered to as factor_val[0][0] NOT
factor_val[1][1] and definitely NOT factor_val[1,0].

var factor_val = new Array();
factor_val[0] = new Array();
factor_val[0][0] = 68.8;
factor_val[0][1] = 55;
factor_val[0][2] = 45.5;
factor_val[0][3] = 42.3;
etc...
factor_val[1] = new Array();
factor_val[1][0] = 36.7;
factor_val[1][1] = 31;
etc...
etc...

I presume you get my drift.

Good luck,
Juliette
 
L

Lasse Reichstein Nielsen

Juliette said:
Evertjan. said:
M.Siler wrote on 16 nov 2003 in comp.lang.javascript:
var factor_val = new Array(8,7)

There are no cats in America,
I mean there are no multidimensional arrays in Javascript.

<script>

var factor_val = new Array(8,7)

alert(factor_val[0]) // shows 8
alert(factor_val[1]) // shows 7

</script>


I don't agree and the NS javascript specs agree with me: there are
multidimensional arrays in Javascript, except you have to define them
slightly differently from what has been done in the sample code.

I disagree with you. There are no multidimensional arrays in
Javascript. There is no single array that is multidimensional. What
you do have is arrays containing arrays, which is a completely
different thing (although it can be used to emulate multidimensional
arrays in their absence).

C has multidimensional arrays. Java doesn't, it only has arrays of
arrays.

This is ofcourse splitting hairs, i.e., purely a matter of
terminology ...
The following comes straight from the Client-Side Javascript Reference
(v 1.3):

** START QUOTE **

Example 2: Two-dimensional array. The following code creates a
two-dimensional array and assigns the results to myVar.

.... and I disagree with their use of terminology.

So to answer Mr Siler's question:
You have to define your array using the following method:

.... a perfectly good advice.
var factor_val = new Array();
factor_val[0] = new Array();
factor_val[0][0] = 68.8;
factor_val[0][1] = 55;
factor_val[0][2] = 45.5;
factor_val[0][3] = 42.3;
etc...
factor_val[1] = new Array();
factor_val[1][0] = 36.7;
factor_val[1][1] = 31;
etc...

You can also use array literals:
var factor_val = [[68.8, 55, 45.5, 42.3, 39.3, 34.4, 30.6],
[36.7, 31, ... ],
...
[..., 2.6, 2.5, 2, 1.7]
];

/L
 
J

Juliette

Lasse said:
Juliette said:
Evertjan. said:
M.Siler wrote on 16 nov 2003 in comp.lang.javascript:

var factor_val = new Array(8,7)


There are no cats in America,
I mean there are no multidimensional arrays in Javascript.

<script>

var factor_val = new Array(8,7)

alert(factor_val[0]) // shows 8
alert(factor_val[1]) // shows 7

</script>


I don't agree and the NS javascript specs agree with me: there are
multidimensional arrays in Javascript, except you have to define them
slightly differently from what has been done in the sample code.

I disagree with you. There are no multidimensional arrays in
Javascript. There is no single array that is multidimensional. What
you do have is arrays containing arrays, which is a completely
different thing (although it can be used to emulate multidimensional
arrays in their absence).

C has multidimensional arrays. Java doesn't, it only has arrays of
arrays.

This is ofcourse splitting hairs, i.e., purely a matter of
terminology ...
The following comes straight from the Client-Side Javascript Reference
(v 1.3):

** START QUOTE **

Example 2: Two-dimensional array. The following code creates a
two-dimensional array and assigns the results to myVar.

... and I disagree with their use of terminology.
So to answer Mr Siler's question:
You have to define your array using the following method:

... a perfectly good advice.
var factor_val = new Array();
factor_val[0] = new Array();
factor_val[0][0] = 68.8;
factor_val[0][1] = 55;
factor_val[0][2] = 45.5;
factor_val[0][3] = 42.3;
etc...
factor_val[1] = new Array();
factor_val[1][0] = 36.7;
factor_val[1][1] = 31;
etc...

You can also use array literals:
var factor_val = [[68.8, 55, 45.5, 42.3, 39.3, 34.4, 30.6],
[36.7, 31, ... ],
...
[..., 2.6, 2.5, 2, 1.7]
];

/L


Lasse,

Of course you are completely correct concerning the terminology, but at
the end of the day the effect is what counts and as desired.

The shorthand array definition which you show is absolutely correct and
much more efficient.
The longhand version just goes to show what is actually being done ;-).

Greetz, Juliette
 
M

M.Siler

Thanks for everyone help... I agree it's not a true multidimensional array,
but it works close enough like one... here's what I ended up with.

-----------------
<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT>
<!--

var row_1 = new Array(68.8, 36.7, 30.6, 24.3, 17.2, 9.7, 7.6, 4)
var row_2 = new Array(55, 31, 26.5, 19.7, 13.7, 7.7, 6, 3)
var row_3 = new Array(45.5, 25.8, 20.4, 16.5, 11.5, 6.5, 5.1, 2.7)
var row_4 = new Array(42.3, 23.8, 18.8, 15.2, 10.6, 5.9, 4.7, 2.6)
var row_5 = new Array(39.3, 22.1, 17.5, 14.1, 9.8, 5.5, 4.4, 2.5)
var row_6 = new Array(34.4, 19.4, 15.3, 12.4, 8.6, 4.8, 3.8, 2)
var row_7 = new Array(30.6, 17.2, 13.6, 11, 7.6, 4.3, 3.4, 1.7)

var M = new Array(row_1, row_2, row_3, row_4, row_5, row_6, row_7)

function compute(form)
{
var myindex_x = form.diameter.selectedIndex;
var myindex_y = form.depth.selectedIndex;

if (myindex_x == 0)
{
alert("Select a Diameter.");
}
else if (myindex_y == 0)
{
alert("Select a Depth.");
}
else
{
form.f_val.value = M[myindex_y-1][myindex_x-1];
form.answer_val.value =
form.num_of_holes.value/M[myindex_y-1][myindex_x-1];
}
}

-->
</SCRIPT>
</HEAD>

<BODY>

<form>

<p><input type="text" name="num_of_holes" size="4" value="0"> Number of
Holes</p>

<p>
<select name="diameter">
<option>Select the</option>
<option>6</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>12</option>
<option>16</option>
<option>18</option>
<option>24</option>
</select> Diameter</p>

<p>
<select name="depth">
<option>Select the</option>
<option>24</option>
<option>30</option>
<option>36</option>
<option>39</option>
<option>42</option>
<option>48</option>
<option>54</option>
</select> Depth</p>

<p><font color="#0000FF"><input type="text" name="f_val" size="4" value="0"
onFocus='this.blur()'> Factor</font></p>
<p><input type="text" name="answer_val" size="6" value="0"
onFocus='this.blur()'> Answer</p>

</p>
<INPUT TYPE="BUTTON" VALUE="Calculate" onClick="compute(this.form)"><input
type="reset" value="Reset" name="B2">
</form>
</BODY>
</HTML>

-----------------

Thanks again!


Juliette said:
Lasse said:
Juliette said:
:

M.Siler wrote on 16 nov 2003 in comp.lang.javascript:

var factor_val = new Array(8,7)


There are no cats in America,
I mean there are no multidimensional arrays in Javascript.

<script>

var factor_val = new Array(8,7)

alert(factor_val[0]) // shows 8
alert(factor_val[1]) // shows 7

</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


I don't agree and the NS javascript specs agree with me: there are
multidimensional arrays in Javascript, except you have to define them
slightly differently from what has been done in the sample code.

I disagree with you. There are no multidimensional arrays in
Javascript. There is no single array that is multidimensional. What
you do have is arrays containing arrays, which is a completely
different thing (although it can be used to emulate multidimensional
arrays in their absence).

C has multidimensional arrays. Java doesn't, it only has arrays of
arrays.

This is ofcourse splitting hairs, i.e., purely a matter of
terminology ...
The following comes straight from the Client-Side Javascript Reference
(v 1.3):

** START QUOTE **

Example 2: Two-dimensional array. The following code creates a
two-dimensional array and assigns the results to myVar.

... and I disagree with their use of terminology.
So to answer Mr Siler's question:
You have to define your array using the following method:

... a perfectly good advice.
var factor_val = new Array();
factor_val[0] = new Array();
factor_val[0][0] = 68.8;
factor_val[0][1] = 55;
factor_val[0][2] = 45.5;
factor_val[0][3] = 42.3;
etc...
factor_val[1] = new Array();
factor_val[1][0] = 36.7;
factor_val[1][1] = 31;
etc...

You can also use array literals:
var factor_val = [[68.8, 55, 45.5, 42.3, 39.3, 34.4, 30.6],
[36.7, 31, ... ],
...
[..., 2.6, 2.5, 2, 1.7]
];

/L
'Faith without judgement merely degrades the spirit divine.'


Lasse,

Of course you are completely correct concerning the terminology, but at
the end of the day the effect is what counts and as desired.

The shorthand array definition which you show is absolutely correct and
much more efficient.
The longhand version just goes to show what is actually being done ;-).

Greetz, Juliette
 
T

Thomas 'PointedEars' Lahn

M.Siler said:

The DOCTYPE declaration is missing for valid HTML.

The character set declaration with the `meta' element is missing for valid HTML.
<TITLE></TITLE>

I do hope this is only a test case since `title' is "the most important
element of an HTML document." (according to the W3C Style Guide.)

The type attribute is missing for valid HTML 4:

[...]
var row_1 = new Array(68.8, 36.7, 30.6, 24.3, 17.2, 9.7, 7.6, 4)
var row_2 = new Array(55, 31, 26.5, 19.7, 13.7, 7.7, 6, 3)
var row_3 = new Array(45.5, 25.8, 20.4, 16.5, 11.5, 6.5, 5.1, 2.7)
var row_4 = new Array(42.3, 23.8, 18.8, 15.2, 10.6, 5.9, 4.7, 2.6)
var row_5 = new Array(39.3, 22.1, 17.5, 14.1, 9.8, 5.5, 4.4, 2.5)
var row_6 = new Array(34.4, 19.4, 15.3, 12.4, 8.6, 4.8, 3.8, 2)
var row_7 = new Array(30.6, 17.2, 13.6, 11, 7.6, 4.3, 3.4, 1.7)

var M = new Array(row_1, row_2, row_3, row_4, row_5, row_6, row_7)

You are wasting memory and polluting the namespace by defining too many
(global) variables. Except of the identifier for the last variable, which
I recommend to start lower-cased and be more descriptive, the following is
equal to the above:

var matrix = new Array(
new Array(68.8, 36.7, 30.6, 24.3, 17.2, 9.7, 7.6, 4),
new Array(55, 31, 26.5, 19.7, 13.7, 7.7, 6, 3),
new Array(45.5, 25.8, 20.4, 16.5, 11.5, 6.5, 5.1, 2.7),
new Array(42.3, 23.8, 18.8, 15.2, 10.6, 5.9, 4.7, 2.6),
new Array(39.3, 22.1, 17.5, 14.1, 9.8, 5.5, 4.4, 2.5),
new Array(34.4, 19.4, 15.3, 12.4, 8.6, 4.8, 3.8, 2),
new Array(30.6, 17.2, 13.6, 11, 7.6, 4.3, 3.4, 1.7));

Or using Array literals:

var matrix = [
[68.8, 36.7, 30.6, 24.3, 17.2, 9.7, 7.6, 4],
[55, 31, 26.5, 19.7, 13.7, 7.7, 6, 3],
[45.5, 25.8, 20.4, 16.5, 11.5, 6.5, 5.1, 2.7],
[42.3, 23.8, 18.8, 15.2, 10.6, 5.9, 4.7, 2.6],
[39.3, 22.1, 17.5, 14.1, 9.8, 5.5, 4.4, 2.5],
[34.4, 19.4, 15.3, 12.4, 8.6, 4.8, 3.8, 2],
[30.6, 17.2, 13.6, 11, 7.6, 4.3, 3.4, 1.7]];

AFAIS the latter (Array literals having Array literals als
list elements) is not specified in ECMAScript Ed. 3 (CMIIW).


PointedEars
 
L

Lasse Reichstein Nielsen

Thomas 'PointedEars' Lahn said:
The DOCTYPE declaration is missing for valid HTML.

HTML 4, that is.
The character set declaration with the `meta' element is missing for valid HTML.

The <meta http-equiv="Content-Type" ... > element is not required by
the HTML specification. The content-type can (should?) be supplied by
the server, that is what "http-equiv" means.
var matrix = [
[68.8, 36.7, 30.6, 24.3, 17.2, 9.7, 7.6, 4],
[55, 31, 26.5, 19.7, 13.7, 7.7, 6, 3],
[45.5, 25.8, 20.4, 16.5, 11.5, 6.5, 5.1, 2.7],
[42.3, 23.8, 18.8, 15.2, 10.6, 5.9, 4.7, 2.6],
[39.3, 22.1, 17.5, 14.1, 9.8, 5.5, 4.4, 2.5],
[34.4, 19.4, 15.3, 12.4, 8.6, 4.8, 3.8, 2],
[30.6, 17.2, 13.6, 11, 7.6, 4.3, 3.4, 1.7]];

AFAIS the latter (Array literals having Array literals als
list elements) is not specified in ECMAScript Ed. 3 (CMIIW).

YAW. (Shouldn't that be "CMIIAW", or are you abbreviating a
contraction?)

The relevant grammar productions are:

ArrayLiteral : [ ElementList ]
ElementList : ElementList , Elison_opt AssignmentExpression
AssignmentExpression : ConditionalExpression
ConditionalExpression : LogicalORExpression
LogicalORExpression : LogicalANDExpression
LogicalANDExpression : BitwiseORExpression
BitwiseORExpression : BitwiseXORExpression
BitwiseXORExpression : BitwiseANDExpression
BitwiseANDExpression : EqualityExpression
EqualityExpression : RelationalExpression
RelationalExpression : ShiftExpression
ShiftExpression : AdditiveExpression
AdditiveExpression : MultiplicativeExpression
MultiplicativeExpression : UnaryExpression
UnaryExpression : PostfixExpression
PostfixExpression : LeftHandSideExpression
LeftHandSideExpression : NewExpression
NewExpression : MemberExpression
MemberExpression : PrimaryExpression
PrimaryExpression : ArrayLiteral

Enjoy :)
/L
 
L

Lasse Reichstein Nielsen

WTFDYJS?

What the F*** did you just say? There are things to be said for abbreviations
when they may not be known to potential readers (I got the YAW, not the other
two)

Ah, the person I responded to finished with "CMIIW", which I decoded as
"correct me if I'm wrong". The answer was ofcourse "YAW" (You are wrong).

If forced to do it, I would abbreviate "correct me if I'm wrong" as
CMIIAW, because I don't like to abbreviate a contraction.

Otherwise, I fully agree and normally try to avoid abbreviations, since
I don't understand all of them either (as a non-native English speaker)

/L
 
T

Thomas 'PointedEars' Lahn

Lasse said:
HTML 4, that is.

It is required for *any* (valid) HTML:

http://www.w3.org/TR/html4/struct/global.html#h-7.2
The <meta http-equiv="Content-Type" ... > element is not required by
the HTML specification. The content-type can (should?) be supplied by
the server, that is what "http-equiv" means.

http://www.w3.org/TR/html4/charset.html#doc-char-set

Validate a local document and you see it is required. It is, because HTML
is not required to be served via HTTP. You are right, the (HTTP) server
should supply the Content-Type along with the character set but if it does
not, the character set of the document differs from the server default (and
the author has no appropriate means to change that for the document) or if
there is no server, a user agent is required to obey the above declaration.
(XHTML is a different thing, of course, the `meta' element comes too late
for the XML parser, it requires the charset declaration within the XML
declaration before the DOCTYPE declaration.)

I like that one :) and I stand corrected.
(Shouldn't that be "CMIIAW",
No.

or are you abbreviating a contraction?)

Yes, and that is Usenet jargon. You may refer to the Jargon File or similar
dictionaries.
The relevant grammar productions are:
[...]

Enjoy :)

Thank you very much for your research. Using the same productions for the
most part[1], I could finally prove that object literals may also be nested
in themselves according to the specification(, a fact that has been denied
by somebody else (and I had not the energy to prove him wrong in those
days)). :)


\V/ Live long and prosper

PointedEars
___________
[1] ObjectLiteral : { PropertyNameAndValueList }
PropertyNameAndValueList : AssignmentExpression
...
PrimaryExpression : ObjectLiteral
 
L

Lasse Reichstein Nielsen

[ said:
It is required for *any* (valid) HTML:

Correct, my mistake.

---
To address server or configuration limitations, HTML documents may
include explicit information about the document's character encoding;
the META element can be used to provide user agents with this
information.
---
Validate a local document and you see it is required.

Not if I tell the validator which character set to use.
It's a good idea to include the character encoding in the document,
but it is not required.
Thank you very much for your research. Using the same productions for the
most part[1], I could finally prove that object literals may also be nested
in themselves according to the specification(, a fact that has been denied
by somebody else (and I had not the energy to prove him wrong in those
days)). :)

It would me very bad design otherwise, if literal compound values could
not be nested, but could be embedded later.


I hope you can prove me mistaken on this one: Function declarations
are only allowed at the top level of programs or function bodies.
That means that

if (true) {
function foo(){}
}

is syntactically incorrect ECMAScript (it can't be an
ExpressionStatement containing a FunctionExpression, since
ExpressionStatements cannot start with "function"), and it
can't be a FunctionDeclaration.

Browsers treat the above as a function declaration.
\V/ Live long and prosper

\V/ \V/ - I am not a nerd!


-L
 
J

John G Harris

I hope you can prove me mistaken on this one: Function declarations
are only allowed at the top level of programs or function bodies.
That means that

if (true) {
function foo(){}
}

is syntactically incorrect ECMAScript (it can't be an
ExpressionStatement containing a FunctionExpression, since
ExpressionStatements cannot start with "function"), and it
can't be a FunctionDeclaration.

Browsers treat the above as a function declaration.

Think sneaky! This ...

<SCRIPT type="text/javascript">

if (2 == 2)
{
document.write("True ");
var f = function () { document.write("Wow!"); };
}
else
document.write("False ");

f();

</SCRIPT>

.... is legal, and does what you were afraid of.

John
 
D

Douglas Crockford

I hope you can prove me mistaken on this one: Function declarations
Whoa. Hold the phone. That is not true. Function statements can appear anywhere
that a statement can appear. And function expressions can appear pretty much
anywhere.

It is syntactically correct. Not only that,

(function (){ })();

is correct, too.
Think sneaky! This ...

There is no need to be sneaky when there are correct alternatives.

http://www.crockford.com/#javascript
 
L

Lasse Reichstein Nielsen

Douglas Crockford said:
Whoa. Hold the phone. That is not true.

I hope so, but I can't convince myself by looking at the ECMAScript
specification (December 1999 version, freshly downloaded from ECMA's
homepage[1]). I can, however, convince myself of the opposite.
Function statements can appear anywhere that a statement can
appear.

FunctionDeclaration can only be produced by SourceElement.
SourceElement can only be produced by SourceElements.
SourceElements can only be produced by Program or FunctionBody.

The productions are:
---
FunctionBody : See clause 13
SourceElements

Program : See clause 14
SourceElements

SourceElements : See clause 14
SourceElement
SourceElements SourceElement
SourceElement : See clause 14
Statement
FunctionDeclaration
---

That means that a function declaration can only occur directly
inside a function body or a program.

Statments in general can also occur inside a block:
---
Block : See 12.1
{ StatementList_opt }
StatementList : See 12.1
Statement
StatementList Statement
---
FunctionDeclarations can not appear here, or most other
places where statements can.

FunctionExpression can appear almost anywhere another expression can,
except at the beginning of an ExpressionStatement (yes, you can wrap
it in parenthese, but if you *don't*, it is not correct - in the
example, I didn't).
And function expressions can appear pretty much anywhere.

Can you show me a derivation of
if (true) {
function foo(){alert();}
foo();
}
in the ECMAScript syntax?

I added to "foo()" to suggest that browsers treat it as a function
declaration. If it is a function expression, then the scope of "foo"
would only be the body of the function. It isn't in Opera 7, IE 6,
Mozilla FB 0.7 or Netscape 4 (the browsers I have tested).

In fact, Opera and IE also accept and run:
if (true) {
foo();
function foo(){alert();}
}
while Mozilla FB and Netscape 4 says that "foo is not defined."
It is syntactically correct.

Prove it. :)
Not only that,

(function (){ })();

is correct, too.

Absolutely, that is a function expression, and the parenthesis at the
beginning makes it a legal expressionStatement. It is a completely
different beast.

Instead of the function declaration:
function foo(){...}
you can write the variable statement:
var foo = function foo(){...};
The only difference is that the function declaration should be
processed before any statements in the same scope are executed,
so you can use a function that is declared later in the code.

/L
[1]<URL:http://www.ecma-international.org/publications/standards/Ecma-262.htm>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top