[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Sort of) newbie trying to understand "array.length"

bit-naughty

2/21/2016 5:51:00 PM

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? And "length" is a property of it? ie. "array" is an object?
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....?

Thanks.
13 Answers

ram

2/21/2016 6:49:00 PM

0

bit-naughty@hotmail.com writes:
>can someone explain to me what "array.length" or "array.map"
>etc. is?

In ECMAScript, these are MemberExpression (12.3).
Their semantics is given in 12.3.1.1.

>Like, "array" is a reserved word which comes with the
>system (Firefox or whatever), right?

»array« is a MemberExpression, too. But it's also
a PrimaryExpression this time. It may have a value.

>And "length" is a property of it?

Yes, it's an identifier that names a possible property.

>ie. "array" is an object?

It's a PrimaryExpression (an IdentifierReference [12.1]),
whose value /can/ be an object, but whose value is not
an object right after the start of Firefox:

array
ReferenceError: array is not defined

>Like, in BASIC

BASIC
ReferenceError: BASIC is not defined

>*JS*, _why_ did they do things this way?

In Java, it is done that way because Brendan Eich was told
that JavaScript should look similar to Java.

Java was designed to look familiar to C++ programmers,
where it is written that way.

In C++, the notation stems from C, where it is used to look
up members of struct and union objects. I don't know where
from it came into C!

>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.....

The »length« property is perceived to /be/ »inside« the
»array« object, when someone writes »array.length«.

|< array = Object.create( null );
|> Object { }
|< Object.defineProperty( array, "length", { value: 27, configurable: true, enumerable: true, writable: true });
|> Object { length: 27 }
|< array.length
|> 27

Thomas 'PointedEars' Lahn

2/21/2016 10:48:00 PM

0

Stefan Ram wrote:

> bit-naughty@hotmail.com writes:
>>Like, "array" is a reserved word which comes with the
>>system (Firefox or whatever), right?
>
> »array« is a MemberExpression, too. But it's also
> a PrimaryExpression this time. It may have a value.

An expression (including a /MemberExpression/) is evaluated to a value
instead. This happens by finding the best applicable production of the
grammar (the longest match usually wins), and executing the implementation
of the algorithm that is specified for that production. This may include
executing implementations of algorithms specified for other productions.

>>And "length" is a property of it?
>
> Yes, it's an identifier that names a possible property.

Grammatically, since ECMAScript Ed. 5 it is an Identifier Name
(/IdentifierName/), meaning that it may be a reserved word (/ReservedWord/)
which is not allowed for /Identifier/s:

<http://www.ecma-international.org/ecma-262/5.1/#s...
<http://www.ecma-international.org/ecma-262/5.1/#se...

<http://www.ecma-international.org/ecma-262/6.0/#sec-names-and-ke...
<http://www.ecma-international.org/ecma-262/6.0/#sec-left-hand-side-expre...

JSHint [1][2] can issue warnings if reserved words are used as property
names in dot property accessor syntax, and if the bracket property accessor
syntax instead of the dot property accessor syntax is used and the property
name is not a reserved word.

[1] <http://jshin...
[2] Recommended: <https://atom.io/packages/linter-...

>>ie. "array" is an object?
>
> It's a PrimaryExpression (an IdentifierReference [12.1]),
> whose value /can/ be an object, but whose value is not
> an object right after the start of Firefox:
>
> array
> ReferenceError: array is not defined

IOW, it is not the name of a variable or a built-in property of an object in
the scope chain in Mozilla JavaScript (unknown version), the version of the
implementation of ECMAScript that is supported by that version of Firefox.

>>Like, in BASIC
>
> BASIC
> ReferenceError: BASIC is not defined

Not funny.

>>*JS*, _why_ did they do things this way?
>
> In Java, it is done that way because Brendan Eich was told
^^^^^^^
> that JavaScript should look similar to Java.

Poorly worded; you have it partially backwards. Brendan Eich did not invent
Java, he invented JavaScript. JavaScript and other ECMAScript
implementations are that way because it was so in Java, not the other way
around.

> Java was designed to look familiar to C++ programmers,

Cite evidence.

> where it is written that way.

As â??arrayâ? would/should hold a *reference* to an object in an ECMAScript
implementation and in Java, in C++ (and C) the equivalent syntax would be
the pointer-dereferencing â??array->lengthâ? or â??(*array).lengthâ? instead.

<http://stackoverflow.com/questions/13366083/why-does-the-arrow-operator-in-c... p.

>>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.....
>
> The »length« property is perceived to /be/ »inside« the
> »array« object, when someone writes »array.length«.

â??arrayâ? is not an object; it is the name of a variable or property whose
value is a reference to an object at the time of successful identifier
resolution.

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

ram

2/22/2016 1:57:00 AM

0

ram@zedat.fu-berlin.de (Stefan Ram) writes:
>>*JS*, _why_ did they do things this way?
>In C++, the notation stems from C, where it is used to look
>up members of struct and union objects. I don't know where
>from it came into C!

Probably PL/1 was the first language to use the now
ubiquitous »pointer.attribute« notation and then Simula 67
followed suite. Before, everybody used to use
»attribute( pointer )«.

Critics of the dot notation say that »attribute( pointer )«
aligns better with mathematical notation because »attribute«
usually is a fixed name, while »pointer« can vary.

However, in »o.f( x, y )«, it is made clear that the
late-binding (run-time polymorphism) only happens with
respect to »o«, not to the other arguments.

Some languages, such as Smalltalk or Lisp, do not use dot
notation. IIRC, Smalltalk does not have multi-methods,
while CLOS might have them. For multimethods, »f( o, x, y )«
might better agree with the semantics.

John Harris

2/22/2016 11:03:00 AM

0

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

Ben Bacarisse

2/22/2016 12:24:00 PM

0

ram@zedat.fu-berlin.de (Stefan Ram) writes:
<snip>
> Critics of the dot notation say that »attribute( pointer )«
> aligns better with mathematical notation because »attribute«
> usually is a fixed name, while »pointer« can vary.
>
> However, in »o.f( x, y )«, it is made clear that the
> late-binding (run-time polymorphism) only happens with
> respect to »o«, not to the other arguments.
>
> Some languages, such as Smalltalk or Lisp, do not use dot
> notation. IIRC, Smalltalk does not have multi-methods,
> while CLOS might have them. For multimethods, »f( o, x, y )«
> might better agree with the semantics.

The obvious extension of the attribute(pointer) syntax the a method call
would be f(o)(x, y) which looks fine to me. (Not that I mind the dot
notation either.)

--
Ben.

Aleksandro

2/22/2016 2:57:00 PM

0

On 21/02/16 15:49, Stefan Ram wrote:
>> ie. "array" is an object?
> It's a PrimaryExpression (an IdentifierReference [12.1]),
> whose value /can/ be an object, but whose value is not
> an object right after the start of Firefox:
>
> array
> ReferenceError: array is not defined

How about `Array`?

ram

2/22/2016 3:21:00 PM

0

Aleksandro <aleksandro@gmx.com> writes:
>How about `Array`?

That's a »template string« in Firefox.

Thomas 'PointedEars' Lahn

2/22/2016 8:19:00 PM

0

Stefan Ram wrote:

> Aleksandro <aleksandro@gmx.com> writes:
>>How about `Array`?
>
> That's a »template string« in Firefox.

It is a template _literal_ in all conforming implementations of ECMAScript
2015.

<http://www.ecma-international.org/ecma-262/6.0/index.html#sec-template-literal-lexical-comp...

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

ram

2/23/2016 3:38:00 AM

0

ram@zedat.fu-berlin.de (Stefan Ram) writes:
>ram@zedat.fu-berlin.de (Stefan Ram) writes:
>>>*JS*, _why_ did they do things this way?
>>In C++, the notation stems from C, where it is used to look
>>up members of struct and union objects. I don't know where
>>from it came into C!
>Probably PL/1 was the first language to use the now
>ubiquitous »pointer.attribute« notation and then Simula 67
>followed suite. Before, everybody used to use
>»attribute( pointer )«.

BTW: In Perl 6, one can write either

"abc".chars

or

chars( "abc" )

as well, to get the number of chars of the string.

Evertjan.

2/23/2016 10:20:00 AM

0

ram@zedat.fu-berlin.de (Stefan Ram) wrote on 23 Feb 2016 in
comp.lang.javascript:

> BTW: In Perl 6, one can write either
> "abc".chars
> or
> chars( "abc" )
> as well, to get the number of chars of the string.

Javascript has next to:

alert( 'abc'.length ); // 3

also:

alert( 'abc'['length'] ); // 3

So also:

var st = 'def';
var le = 'length';
alert( st[le] ); // 3


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