V
VK
(see the post by ASM in the original thread; can be seen at
<http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3716384d8bfa1b0b>
as an option)
As that is not in relevance to "new Array() vs []" question or to the
array performance, I dared to move it to a new thread.
Gecko takes undefined value strictly as per Book 4, Chapter 3, Song 9
of Books of ECMA
(Paragraph 4.3.9 of ECMAScript Language Specification, 3rd edition)
:-|
<quote> The undefined value is a primitive value used when a variable
has not been assigned a value. </quote>
As a matter complication JavaScript is the only language I know where
one can write:
var foo = undefined;
which is - strictly speaking - "keep foo but assign it the value that
foo was not assigned and never existed" which is - even more strictly
speaking - totally meaningless.
In case of an elision in an array initializer you *do* assign values to
each and every array member (Mr. Cornford would say something like
"[[Put]] method is called for each evaluation result").
This way
var arr = ['A',,'B']
really means
var arr = ['A',undefined,'B']
with three array members explicetly assigned to the array on the
initialization stage. And each and every *assigned* array member is
reflected as an enumerable object property of the underlaying Object
object. To see the difference (and the real sense of undefined value)
try this:
<script type="text/javascript">
function f(arg) {
for (var p in arg) {
window.alert(arg[p] || 'undefined');
}
window.alert('loops: ' + i);
}
function demo() {
var arr = ['A',,'B'];
arr[100] = 'C';
f(arr);
}
window.onload = demo;
</script>
arr[1] member gets an explicit assignment; thus then you treat your
array as an Object type, you see it as property "1" with value
undefined.
After arr[100] = 'C'; you're getting 96 members gap to the next defined
value. But arr[3]...arr[99] never participated in any assignment
operations. They are undefined and existing only in the abstract array
continuum space. Therefore they are not reflected as object properties.
<http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3716384d8bfa1b0b>
as an option)
As that is not in relevance to "new Array() vs []" question or to the
array performance, I dared to move it to a new thread.
Gecko takes undefined value strictly as per Book 4, Chapter 3, Song 9
of Books of ECMA
(Paragraph 4.3.9 of ECMAScript Language Specification, 3rd edition)
:-|
<quote> The undefined value is a primitive value used when a variable
has not been assigned a value. </quote>
As a matter complication JavaScript is the only language I know where
one can write:
var foo = undefined;
which is - strictly speaking - "keep foo but assign it the value that
foo was not assigned and never existed" which is - even more strictly
speaking - totally meaningless.
In case of an elision in an array initializer you *do* assign values to
each and every array member (Mr. Cornford would say something like
"[[Put]] method is called for each evaluation result").
This way
var arr = ['A',,'B']
really means
var arr = ['A',undefined,'B']
with three array members explicetly assigned to the array on the
initialization stage. And each and every *assigned* array member is
reflected as an enumerable object property of the underlaying Object
object. To see the difference (and the real sense of undefined value)
try this:
<script type="text/javascript">
function f(arg) {
for (var p in arg) {
window.alert(arg[p] || 'undefined');
}
window.alert('loops: ' + i);
}
function demo() {
var arr = ['A',,'B'];
arr[100] = 'C';
f(arr);
}
window.onload = demo;
</script>
arr[1] member gets an explicit assignment; thus then you treat your
array as an Object type, you see it as property "1" with value
undefined.
After arr[100] = 'C'; you're getting 96 members gap to the next defined
value. But arr[3]...arr[99] never participated in any assignment
operations. They are undefined and existing only in the abstract array
continuum space. Therefore they are not reflected as object properties.