包含非数字键的 JavaScript 数组的正确术语是什么?
What is the proper terminology for a JavaScript array that includes non-numeric keys?
我正在寻找 "bad practice" 以外的答案。
PLEASE, this is a question about TERMINOLOGY only! This is NOT a question about alternative ways this could be done, why it's better to use an object or Map instead of an array, why the length property of the array and the number of keys are different, etc.
术语 "associative array" 似乎通常用于用花括号声明的对象,但随后用方括号访问,如下所示:
const a = {'not-a-proper-identifier': 'foo'};
a['not-a-proper-identifier'] // returns 'foo'
但是你可以这样做:
const a = [1, 2, 3];
a['foo'] = 'bar';
a['foo']; // Yep, 'bar' comes back.
console.log(a); // Output (at least from Chrome) is [1, 2, 3, foo: "bar"]
Array.isArray(a); // This is still true
a.length; // 3
Object.keys(a).length; // 4
JSON.stringify(a); // Only returns [1,2,3], 'foo' disappears.
[1, 2, 3, foo: "bar"] // Syntax error! The console representation isn't proper JavaScript.
所以,如果你要用一个数组来做这个,有什么特别的东西可以称呼这种数组吗?
我为什么要问?
不是因为我特别想自己使用这个数据结构,而是因为我希望能够支持序列化和反序列化这样的数组,如果遇到的话,作为 JSON 的变体,我正在进行 JSON-Z, a fork off the JSON5 项目。
如果我要支持它,那么在引用这样的数组时知道使用什么术语会很好。
只是"object"。 obj['foo']
等价于 obj.foo
.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
让我们照写的那样,将非数字键添加到数组中不是一个好主意;评论清楚地同意这一点,并且从您提供的代码示例中可以看出这一点。从表面上看,语言很难始终如一地处理这个问题。
你写的代码的解释可能没有必要,但我觉得尽可能清楚地说明为什么 javascript 中存在一些表面上的不一致是很好的,以便更好地理解语言有效。
正在检查您提供的示例:
const a = [1, 2, 3];
a
是一个数组,但是数组是javascript.
中的对象
a['foo'] = 'bar';
您可以设置对象的属性。
a['foo'];
这确实是bar
;我们只是在对象
上访问 属性
console.log(a); // Output (at least from Chrome) is [1, 2, 3, foo: "bar"]
Chrome 似乎在解释如何将其记录到控制台方面做了令人钦佩的工作,它也可以将其作为标准对象记录出来。
Array.isArray(a); // This is still true
这是真的; Array.isArray
checks the Class internal property in order to determine what is an array.
a.length; // 3
长度计算为最大数字键 + 1
JSON.stringify(a); // Only returns [1,2,3], 'foo' disappears.
确实 - JSON 无法处理这种键的混合(你可能知道,考虑到你正在做一个扩展 JSON 的项目)所以它必须决定是否解析作为 json 对象或 json 数组的对象。
控制台表示实际上只是尽可能好的表示所记录的数据,并且考虑到 javascript 对象的混合性质 - 用于数组和字典,实际上很难获得数据的完美表现。
至于您的实际问题...我一直没能找到这些对象属性的名称。我会说我能想到的最好的描述是:
Object properties which do not conform to standard javascript naming
syntax
这不是特别好,但似乎您问题的实际答案很简单不,这些没有名称。
我正在寻找 "bad practice" 以外的答案。
PLEASE, this is a question about TERMINOLOGY only! This is NOT a question about alternative ways this could be done, why it's better to use an object or Map instead of an array, why the length property of the array and the number of keys are different, etc.
术语 "associative array" 似乎通常用于用花括号声明的对象,但随后用方括号访问,如下所示:
const a = {'not-a-proper-identifier': 'foo'};
a['not-a-proper-identifier'] // returns 'foo'
但是你可以这样做:
const a = [1, 2, 3];
a['foo'] = 'bar';
a['foo']; // Yep, 'bar' comes back.
console.log(a); // Output (at least from Chrome) is [1, 2, 3, foo: "bar"]
Array.isArray(a); // This is still true
a.length; // 3
Object.keys(a).length; // 4
JSON.stringify(a); // Only returns [1,2,3], 'foo' disappears.
[1, 2, 3, foo: "bar"] // Syntax error! The console representation isn't proper JavaScript.
所以,如果你要用一个数组来做这个,有什么特别的东西可以称呼这种数组吗?
我为什么要问?
不是因为我特别想自己使用这个数据结构,而是因为我希望能够支持序列化和反序列化这样的数组,如果遇到的话,作为 JSON 的变体,我正在进行 JSON-Z, a fork off the JSON5 项目。
如果我要支持它,那么在引用这样的数组时知道使用什么术语会很好。
只是"object"。 obj['foo']
等价于 obj.foo
.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
让我们照写的那样,将非数字键添加到数组中不是一个好主意;评论清楚地同意这一点,并且从您提供的代码示例中可以看出这一点。从表面上看,语言很难始终如一地处理这个问题。
你写的代码的解释可能没有必要,但我觉得尽可能清楚地说明为什么 javascript 中存在一些表面上的不一致是很好的,以便更好地理解语言有效。
正在检查您提供的示例:
const a = [1, 2, 3];
a
是一个数组,但是数组是javascript.
a['foo'] = 'bar';
您可以设置对象的属性。
a['foo'];
这确实是bar
;我们只是在对象
console.log(a); // Output (at least from Chrome) is [1, 2, 3, foo: "bar"]
Chrome 似乎在解释如何将其记录到控制台方面做了令人钦佩的工作,它也可以将其作为标准对象记录出来。
Array.isArray(a); // This is still true
这是真的; Array.isArray
checks the Class internal property in order to determine what is an array.
a.length; // 3
长度计算为最大数字键 + 1
JSON.stringify(a); // Only returns [1,2,3], 'foo' disappears.
确实 - JSON 无法处理这种键的混合(你可能知道,考虑到你正在做一个扩展 JSON 的项目)所以它必须决定是否解析作为 json 对象或 json 数组的对象。
控制台表示实际上只是尽可能好的表示所记录的数据,并且考虑到 javascript 对象的混合性质 - 用于数组和字典,实际上很难获得数据的完美表现。
至于您的实际问题...我一直没能找到这些对象属性的名称。我会说我能想到的最好的描述是:
Object properties which do not conform to standard javascript naming syntax
这不是特别好,但似乎您问题的实际答案很简单不,这些没有名称。