为什么JavaScript允许数组语法访问属性? [英] Why does JavaScript allow array syntax to access properties?

查看:54
本文介绍了为什么JavaScript允许数组语法访问属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

密切相关的问题:

我有以下代码示例:

var someVariable = {abc: "def", ghi: "jkl"}

someVariable["SomeProperty"] = "dfsdfadsd"

alert(someVariable["SomeProperty"])

var someOtherVariable = { abcd: "defsd", ghij: "dfsdfdss" }

someOtherVariable.SomeOtherProperty = "adfsdfsd"

alert(someOtherVariable.SomeOtherProperty);

alert(someOtherVariable["SomeOtherProperty"])

它们全部按照它们的外观做-将属性附加到各自的对象上. alert 会在每种情况下显示预期的字符串.从相关问题中我的理解是,两者之间没有功能上的差异.

All of them do exactly what they look like - they attach a property to their respective objects. The alert shows the expected string in every case. My understanding from the related questions is that there's no functional difference between the two.

我还在W3Schools的 JavaScript数组教程中遇到了以下语句:

I also encountered the following statements in W3Schools's JavaScript arrays tutorial:

许多编程语言都支持带有命名索引的数组.

Many programming languages support arrays with named indexes.

具有命名索引的数组称为关联数组(或哈希).

Arrays with named indexes are called associative arrays (or hashes).

JavaScript 不支持具有命名索引的数组.

JavaScript does not support arrays with named indexes.

在JavaScript中,数组始终使用编号索引.

In JavaScript, arrays always use numbered indexes.  

如果是这种情况,为什么在这里完全允许使用数组语法?(再次,从相关问题中我的理解是,关联数组和JavaScript对象之间的实际区别至少是稍微模糊的,这就是JavaScript在幕后"实现对象属性的方式.)

If that's the case, why is array syntax permitted here at all? (Again, my understanding from the related questions is that the actual distinction between an associative array and a JavaScript object is, at a minimum, slightly blurry, and that that's how JavaScript is implementing object properties "under the hood").

据我所知(如果我不正确,因为JavaScript不是我的主要语言,请纠正我),不可能做其他您希望可以对数组进行的操作(例如迭代)和 for 循环),那么为什么要同时使用两种语法呢?在C#中,您可以执行以下操作:

As far as I know (and please correct me if I'm wrong as JavaScript isn't my primary language), it's not possible to do other things you'd expect to be able to do with an array (e.g. iteration with a for loop), so why bother having both syntaxes? In C# you can do something like:

// A C# dictionary is basically an associative array
Dictionary<string, int> dict = new Dictionary<string, int>();
// Fill dictionary ...
foreach (string key in dict.Keys) {
    int value = dict[key];
    // Do something with the value
}

但是我不知道使用JavaScript属性做类似事情的方法.在C#中显然有必要使用这种语法(键绝对是 dict not 属性),但是JavaScript为什么具有此功能(假设它们完全等效)?我是否缺少某些东西,或者这实际上是完全多余的吗?

but I'm not aware of a way to do a similar thing with JavaScript properties. There's a clear necessity for this syntax in C# (keys are definitely not properties of dict), but why does JavaScript have this (given that they're exactly equivalent)? Am I missing something, or is this actually completely redundant?

推荐答案

为什么JavaScript允许数组语法访问属性?

Why does JavaScript allow array syntax to access properties?

它不是数组语法".

方括号是访问对象属性的标准方法.

Square brackets are a standard way to access the properties of an object.

数组只是一种对象.

方括号表示法与点表示法相比,在访问属性方面具有一些优势:

Square bracket notation has some advantages over dot notation for accessing properties:

  • 您可以使用任何表达式来定义名称,包括变量和函数调用.
  • 您可以访问名称在标识符中无效的属性(例如,以数字开头的属性,这就是为什么您通常会看到它们用于访问数组的原因).

它也更冗长,而且效率可能更低.

It's also more verbose and potentially less efficient.

关联数组和JavaScript对象之间的实际区别至少是有点模糊,这就是JavaScript在幕后"实现对象属性的方式.

the actual distinction between an associative array and a JavaScript object is, at a minimum, slightly blurry, and that that's how JavaScript is implementing object properties "under the hood"

JavaScript没有称为关联数组"的功能.(W3Schools并非可信赖的消息来源.)

JavaScript doesn't have a feature called "associative arrays". (W3Schools is not a trustworthy source).

它有一些对象,它们是(属性)值对(属性)对的集合.(这与PHP的关联数组功能 类似).

It has objects, which are (at their core) collections of property:value pairs. (This is similar to PHP's associative array feature).

它具有数组,这些数组是从Array构造函数继承的对象,并获得诸如 forEach 之类的方法以及诸如 length 之类的属性(该属性使用getter函数根据其值来确定其值).名称上具有最高整数值的属性).

It has arrays, which are objects which inherit from the Array constructor, gaining methods like forEach and properties like length (which uses a getter function to determine its value based on the properties with a name that is the highest integer value).

这篇关于为什么JavaScript允许数组语法访问属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆