Creating a 2d array of objects in javascript

I have a javascript object - cell{xPos, yPos}; I would like to create a 2d array of this object. cellPrototype = function(x, y) { this.xPos = x; this.yPos = y; } var cell = new Array(); for(var i=0;i<10;i++) { cell[i] = new Array(); for(var j=0;j<10;j++) { cell[i][j] = new cellPrototype(i,j); } } This code doesn't work. Neither does - var cellPrototype = function(x, y) { return { xPos : x; yPos : y; } var cell = new Array(); for(var i=0;i<10;i++) { cell[i] = new Array(); for(var j=0;j<10;j++) { cell[i][j] = new cellPrototype(i,j); } } So how do I create a 2d array of an object in javascript?
Unless I've misunderstood your question, your first example seems to work fine: jsfiddle.net/tgz22

以上就是Creating a 2d array of objects in javascript的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏