在javascript中从队列中提取值
我已经用java脚本创建了一个队列,它以以下方式存储在javascript中从队列中提取值,javascript,Javascript,我已经用java脚本创建了一个队列,它以以下方式存储 function MyType(coords, val) { this.coords = coords; this.val = val; } function Couple(x, y) { this.x = x; this.y = y ; } var queue = []; villianXPos = 4; villianYPos = 4; queue.push(new MyType(new Couple(villi
function MyType(coords, val) {
this.coords = coords;
this.val = val;
}
function Couple(x, y) {
this.x = x;
this.y = y ;
}
var queue = [];
villianXPos = 4;
villianYPos = 4;
queue.push(new MyType(new Couple(villianXPos,villianYPos, 0)));
现在我想在单独的变量中提取坐标和val
当我这样做的时候
var element = queue.shift();
元素正在获取整个值4,4,0
是否可能在我的元素变量中只得到4,4,而在另一个变量中得到0 假设
queue.push(new MyType(new Couple(villianXPos,villianYPos, 0)));
实际上应该是
queue.push(new MyType(new Couple(villianXPos,villianYPos), 0));
然后
但是,使用getter/setter进行此类对象访问是一种很好的做法u可以根据需要为每个var分配数组a值似乎您期望
队列。push
将放入[[villanXPos,villanYPos],0]
?@MZZZB井至少显示10,20,0警报(元素)。看来它就是这么做的。你可以暂时忽略villianXPos,villianYPos新人(villianXPos,villianYPos,0)
,0
从未被使用过。@wared我编辑了问题thks…我在queue.push中出错了(new MyType(new Couple(villianXPos,villianYPos,0));
function MyType(coords, val) {
this.coords = coords;
this.val = val;
}
function Couple(x, y) {
this.x = x;
this.y = y ;
}
var queue = [];
villianXPos = 4;
villianYPos = 4;
queue.push(new MyType(new Couple(villianXPos,villianYPos), 0));
var meh = queue.shift();
console.log(meh.coords);
//yields Couple {x: 4, y: 4}
console.log(meh.coords.x);
//yields 4
console.log(meh.val);
//yields 0
console.log(queue);
//yields the remainder of the queue, in this case empty array []