简体   繁体   English

在 javascript 中命名对象的数组中 - 名称是否可访问?

[英]In an array of named objects in javascript - are the names accessible?

If we create some objects, and fill an array with those objects, are the names stored within the array, or only the properties of the object?如果我们创建一些对象,并用这些对象填充一个数组,名称是存储在数组中,还是仅存储对象的属性? I guess this may be trivial, but I haven't been able to find an answer.我想这可能是微不足道的,但我一直无法找到答案。

var boxA = {color: "red", width: 100};
var boxB = {color: "yellow", width: 200};
var boxC = {color: "blue", width: 300};

boxArray = [boxA, boxB, boxC];

for (var i = 0; i < boxArray.length; i++) {

    //****
    // What code do we insert here to log
    // boxA
    // boxB
    // boxC
    //****

}

Of course, it is a trivial workaround to add当然,添加是一个微不足道的解决方法

boxA.box = boxA; 

etc and then call等然后打电话

console.log(boxArray[i].box);

But is that really necessary?但这真的有必要吗?

To answer your question directly - no, you can't do what you're asking. 要直接回答您的问题-不,您不能做您要问的事情。 I've run into the same scenario a few times. 我已经遇到过几次相同的情况。 Here's what I've done. 这就是我所做的。 Instead of using an array, you could just add your objects to an object literal instead, and map the object to some unique key, such as an id. 除了使用数组之外,您还可以将对象添加到对象文字中,然后将该对象映射到某些唯一键(例如id)。

var boxes = {
  'boxA': {color: 'red', width: 100},
  'boxB': {color: 'blue', width: 200}, 
  'boxC': {color: 'yellow', width: 300}
}

for(var boxKey in boxes) {
    console.log(boxKey);
}

// to use
boxes.boxA; // do something with boxA

No, that does not work like that. 不,那不是那样的。

The variable name is a reference to an object in a heap area of memory managed by JS automatically for you. 变量名称是由JS自动为您管理的内存区域中的对象的引用

In details it means that: 详细而言,这意味着:

var boxA = {color: "red", width: 100};

this statement: 这个说法:

  1. Creates an object in the heap 在堆中创建一个对象
  2. Associates a local symbol boxA with that object. 将本地符号boxA与该对象关联。

So the object is referenced by one variable yet. 因此,该对象尚未被一个变量引用。

var boxArray = [boxA];

here: 这里:

  1. An array with one element is created. 创建具有一个元素的数组。 That element contains a reference to an object. 该元素包含对对象的引用。 A copy of the reference to be precise. 引用的副本应准确无误。 So, the same original object is referenced twice now. 因此,同一原始对象现在被引用两次。
  2. A boxArray is assigned a reference to the array, which is also placed in the heap. boxArray分配了对该数组的引用,该引用也放置在堆中。

To summarize: the variable names exist only in code listing for developers to easier reason about some objects in memory, instead of operating with memory addresses (which would be horrible). 总结一下:变量名仅存在于代码清单中,以使开发人员能够更轻松地推断出内存中的某些对象,而不是使用内存地址进行操作(这很可怕)。

Well the boxArray is filled with values of variables you are putting in it. 好吧,boxArray充满了您要放入其中的变量的值。 Example: If you would save three integer variables not the names of variables. 示例:如果要保存三个整数变量,而不是变量名。 So your new boxArray is equal to: 因此,您的新boxArray等于:

boxArray = [{color: "red", width: 100},{color: "yellow", width: 200},{color: "blue", width: 300}];

如果要获取对象的键,请尝试Object.keys(object)

Object.keys(boxA)
 ["color", "width"] 

Your variable names are not accessible to the executing code, but if you need to be able to do this you can nest the objects: 执行代码无法访问您的变量名,但是如果您需要执行此操作,则可以嵌套对象:

 var boxes = { boxA: { color: "red", width: 100 }, boxB: { color: "yellow", width: 200 }, boxC: { color: "blue", width: 300 } }; Object.keys(boxes).forEach(function(key) { console.log(key) // boxA, boxB, boxC console.log(boxes[key]) // {color: "red", width: 100}, etc. }); 

Late to the Party but... Since ES6 javascript introduced classes.迟到了,但是……自从 ES6 javascript 引入了类。 If classes is an option you could do:如果类是一个选项,你可以这样做:

class boxA { constructor() { this.color = "red"; this.width = 100; } };
class boxB { constructor() { this.color = "yellow"; this.width = 200; } };
class boxC { constructor() { this.color = "blue"; this.width = 300; } };

let boxArray = [new boxA(), new boxB(), new boxC()];

for (var i = 0; i < boxArray.length; i++) {
  console.log(boxArray[i].constructor.name);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM