[英]Referencing an object in an array of objects in javascript
var Bar = function(){
}
Bar.prototype.drawBar = function(obj){
}
var bars = [];// Creating array
for (i = 0;i < ar.length ;i++){
bars[i] = [new Bar(ar[i],i)]; // Creating array of objects
bars[i].drawBar(i);// I am looking for something like this, Currently it doesn't work
}
I have something like this, How do I draw a bar, giving a reference/index, It should draw with the measurements我有这样的东西,如何绘制一个条形图,给出参考/索引,它应该用测量值绘制
You are trying to use bars[i]
as if it was a Bar
object, but it's an array of objects.您正在尝试将
bars[i]
用作Bar
object,但它是一个对象数组。
Use this to access the first object in the array:使用它来访问数组中的第一个 object:
bars[i][0].drawBar(i);
If you don't have any use for an array of arrays, just store the objects in the array directly:如果你对 arrays 的数组没有任何用处,直接将对象存储在数组中:
bars[i] = new Bar(ar[i],i);
Then your original code to access the object works.然后您访问 object 的原始代码有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.