Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #29644 > unrolled thread

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

Started bybit-naughty@hotmail.com
First post2016-02-21 09:51 -0800
Last post2016-02-23 11:20 +0100
Articles 10 — 8 participants

Back to article view | Back to comp.lang.javascript


Contents

  (Sort of) newbie trying to understand "array.length" bit-naughty@hotmail.com - 2016-02-21 09:51 -0800
    Re: (Sort of) newbie trying to understand "array.length" Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-02-21 23:48 +0100
      Re: (Sort of) newbie trying to understand "array.length" Scott Sauyet <scott.sauyet@gmail.com> - 2016-02-25 06:24 -0800
    Re: (Sort of) newbie trying to understand "array.length" John Harris <niam@jghnorth.org.uk.invalid> - 2016-02-22 11:03 +0000
    Re: (Sort of) newbie trying to understand "array.length" Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-02-22 12:23 +0000
    Re: (Sort of) newbie trying to understand "array.length" Aleksandro <aleksandro@gmx.com> - 2016-02-22 11:57 -0300
      Re: (Sort of) newbie trying to understand "array.length" Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-02-22 21:19 +0100
        Re: (Sort of) newbie trying to understand "array.length" "Christoph M. Becker" <cmbecker69@arcor.de> - 2016-02-23 11:22 +0100
          Re: (Sort of) newbie trying to understand "array.length" Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-02-23 20:50 +0100
    Re: (Sort of) newbie trying to understand "array.length" "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-02-23 11:20 +0100

#29644 — (Sort of) newbie trying to understand "array.length"

Frombit-naughty@hotmail.com
Date2016-02-21 09:51 -0800
Subject(Sort of) newbie trying to understand "array.length"
Message-ID<b99c7479-d78d-4e99-b6e7-c3e6d2010e6c@googlegroups.com>
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.

[toc] | [next] | [standalone]


#29646

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2016-02-21 23:48 +0100
Message-ID<2392050.Ox0AAuCGCL@PointedEars.de>
In reply to#29644
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/#sec-7.6>
<http://www.ecma-international.org/ecma-262/5.1/#sec-11.2>

<http://www.ecma-international.org/ecma-262/6.0/#sec-names-and-keywords>
<http://www.ecma-international.org/ecma-262/6.0/#sec-left-hand-side-expressions>

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://jshint.com/>
[2] Recommended: <https://atom.io/packages/linter-jshint>
 
>>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-exist> 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.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#29687

FromScott Sauyet <scott.sauyet@gmail.com>
Date2016-02-25 06:24 -0800
Message-ID<7cbb8f88-95b8-4287-b59e-d60f2ba21b14@googlegroups.com>
In reply to#29646
Thomas 'PointedEars' Lahn wrote:
> Stefan Ram wrote:
>> bit-naughty@hotmail.com wrote:

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

Why do you insist on this when it's well known in the programming community
and easy to find (see, e.g., [1]) and yet you assert without citing any
evidence at all, "JavaScript and other ECMAScript implementations are that
way because it was so in Java"?

Of course that's correct too, but why do you think that Stefan must cite
evidence and you're exempt from the same?

Your correction of Stefan about the Java/JavaScript history was _a propos_.

Why bother following it with such trivial and insulting nonsense?


  [1]: <http://www.oracle.com/technetwork/java/simple-142339.html>, which 
       says, in part, 

       | Another major design goal is that Java look _familiar_ to a 
       | majority| of programmers in the personal computer and workstation 
       | arenas, where a large fraction of system programmers and 
       | application programmers are familiar with C and C++. Thus, Java
       | "looks like" C++.


  -- Scott

[toc] | [prev] | [next] | [standalone]


#29649

FromJohn Harris <niam@jghnorth.org.uk.invalid>
Date2016-02-22 11:03 +0000
Message-ID<qkqlcbliek4mmhbph9rt3jl1dsp8ml264o@4ax.com>
In reply to#29644
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

[toc] | [prev] | [next] | [standalone]


#29652

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2016-02-22 12:23 +0000
Message-ID<8760xhylv6.fsf@bsb.me.uk>
In reply to#29644
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.

[toc] | [prev] | [next] | [standalone]


#29658

FromAleksandro <aleksandro@gmx.com>
Date2016-02-22 11:57 -0300
Message-ID<naf7eq$4iv$1@dont-email.me>
In reply to#29644
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`?

[toc] | [prev] | [next] | [standalone]


#29664

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2016-02-22 21:19 +0100
Message-ID<1755220.aeVMpFuXr9@PointedEars.de>
In reply to#29658
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-components>

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

[toc] | [prev] | [next] | [standalone]


#29670

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2016-02-23 11:22 +0100
Message-ID<nahbsl$jrp$1@solani.org>
In reply to#29664
Thomas 'PointedEars' Lahn wrote:

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

Well, in this case it's just a way to mark the term "Array" as literal code.

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#29671

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2016-02-23 20:50 +0100
Message-ID<1589624.V5keUiXcoG@PointedEars.de>
In reply to#29670
Christoph M. Becker wrote:

> Thomas 'PointedEars' Lahn wrote:
>> 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-components>
> 
> Well, in this case it's just a way to mark the term "Array" as literal
> code.

It depends on the interpretation of the question.  Without context (and I am 
not seeing, much less reading, the troll’s postings directly), both 
interpretations are valid.

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

[toc] | [prev] | [next] | [standalone]


#29669

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2016-02-23 11:20 +0100
Message-ID<XnsA5B7735A697E0eejj99@194.109.6.166>
In reply to#29644
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)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web