[英]Access elements of an array or array-like object using the dot notation
It would be array-like object,but why we can't access it with dot notation instead of bracket notation? 它会是类似数组的对象,但是为什么我们不能用点表示法而不是方括号表示法来访问它?
function testArray (rat){
return typeof arguments;
}
console.log(testArray("test")); // object
function testArray (rat){
return arguments.0; //[0] is work
}
console.log(testArray("test")); // error
Your question seems to be about why we can't access array and array-like elements using the dot notation like this 您的问题似乎是关于为什么我们不能使用这样的点符号来访问数组和类似数组的元素
var v = a.0;
Then, it's described in the ECMAScript specification : 然后, 在ECMAScript规范中对其进行了描述:
The dot notation is explained by the following syntactic conversion:
点符号通过以下语法转换进行解释:
MemberExpression .
MemberExpression。 IdentifierName
IdentifierName
And identifiers may not start with a digit as described here : 和标识符不能以数字开头如下描述 :
IdentifierName ::
IdentifierName ::
IdentifierStart
IdentifierStart
IdentifierName IdentifierPart
IdentifierName IdentifierPart
IdentifierStart ::
IdentifierStart ::
UnicodeLetter
UnicodeLetter
$
$
_
_
\\ UnicodeEscapeSequence
\\ 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.
可能只为数组访问设计了一个例外,但这会使语言更加复杂,并脱离了通用的C系列语法,而没有任何实际收获。
You can: 您可以:
var arr = [];
arr.foo = 'foo';
console.log(arr.foo); // => 'foo'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.