![](/img/trans.png)
[英]How to Set a Javascript Object Key to Be Another Object's Value
[英]What is the type of the key when you set an object's key to be another object?
在 JS 中,当您将对象的属性键设置为其他对象时,键是什么? 它是作为字符串的对象的内存引用吗? 还有什么?
例子:
// Let's create some apples.
const apple1 = { tastes: 'great' };
const apple2 = { tastes: 'so-so' };
// Let's create an orchard to hold our apples, with constant time look-up for any individual apple, so we're not using an array.
const orchard = {}
// And we add our first apple to the orchard. We want to keep track of some data.
orchard[apple1] = 1;
orchard[apple2] = 2;
此时,console.logging orchard 的keys 会显示[object Object]
。 控制台记录键的类型会告诉你 [object Object] 是一个字符串。 但是我可以有多个 [object Object] 键,而且它们都是不同的。
在 JavaScript 中,所有对象键都是字符串。
设置键时,如果值不是字符串,将调用 .toString() 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.