John Harris
2/22/2016 11:03:00 AM
On Sun, 21 Feb 2016 09:51:04 -0800 (PST), bit-naughty@hotmail.com
wrote:
>Well OK, I'm not really a Newbie, been reading my JS book for a while now, and puzzling over some of the stuff..... - can someone explain to me what "array.length" or "array.map" etc. is? Like, "array" is a reserved word which comes with the system (Firefox or whatever), right?
No, not right in the current Firefox. "array" is just an ordinary, but
confusing, identifier. If you want an array object you can do
var ar = new Array();
for example. (And there are other ways to do it).
> And "length" is a property of it? ie. "array" is an object?
In the example above ar is an array object and ar.length is the
value of its length property.
>Like, in BASIC, if a$ is "dog" then LEN a$ will be "3", a child of 10 can understand that...... in PHP I think it's "strlen", but in *JS*, _why_ did they do things this way? Why give us something with a dot in it, to make life harder for everyone? To *my* mind, array-dot-something should be what's INSIDE that array, like a subscript, just like a dot after an object literal ("x.d") means what's INSIDE that object literal, but that's not what they did..... subscripts are [ and ] (big brackets)..... the whole thing is just confuzzling me, can anyone explain....?
Perhaps you weren't aware that in ECMAScript the length property
doesn't have to tell you the number of elements in the array object.
Firstly ES arrays are sparse arrays. You can do
ar[4e9] = 42;
and the browser won't mind even if it hasn't been given more than 32GB
to play with. The program only remembered that one entry.
ar.length automatically increased to 4e9 + 1 when this was done.
Secondly, if you now do
ar[6] = 27;
ar.length = 19;
then all elements at ar[19] upward are deleted, ar[6] is the only
remaining element, and the length value is 19.
John