1

Why can’t we access this array-like object’s properties with dot notation instead of bracket notation?

function testArray(rat){
  return typeof arguments;
}

console.log(testArray("test")); // "object"

function testArray(rat){
  return arguments.0; // `arguments[0]` works.
}

console.log(testArray("test")); // Throws error.
2

2 Answers 2

6

Your question seems to be about why we can’t access array and array-like elements using the dot notation like this:

const v = a.0;

It’s described in the ECMAScript specification:

The dot notation is explained by the following syntactic conversion:

MemberExpression . IdentifierName

And identifiers may not start with a digit as described here:

IdentifierName ::
IdentifierStart
IdentifierName IdentifierPart
IdentifierStart ::
UnicodeLetter
$
_
\ UnicodeEscapeSequence

As for the reasoning, having identifier names just being made of digits would have made it difficult to write number literals. An exception could probably have been designed just for array access but that would have made the language more complex and departing from the common C family syntax without any real gain.

Sign up to request clarification or add additional context in comments.

Comments

-1

You can:

var arr = [];
arr.foo = 'foo';

console.log(arr.foo); // => 'foo'

3 Comments

@WouterHuysentruit Sure, but it was a hard trip :)
I doubt it. It doesn't look like a pertinent use of an array-like object.
@dystroy Of course! But his question left me alone with my brain, and that's the result ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.