这个JavaScript语法对于.unshift()意味着什么?

这个JavaScript语法对于.unshift()意味着什么?,javascript,Javascript,我在教程中看到了这段代码,我不知道unshift方法中发生了什么。我理解.unshift()在js中的作用,但我不理解这个语法在做什么,特别是它被写成x:x和y:y的事实 insert: function(x, y) { this._queue.unshift({x:x, y:y}); // unshift prepends an element to array this.last = this._queue[0]; }, 它正在向数组中添加一个对象,

我在教程中看到了这段代码,我不知道unshift方法中发生了什么。我理解.unshift()在js中的作用,但我不理解这个语法在做什么,特别是它被写成x:x和y:y的事实

insert: function(x, y) {
        this._queue.unshift({x:x, y:y}); // unshift prepends an element to array
        this.last = this._queue[0];
    },

它正在向数组中添加一个对象,因此,如果该数组以前为空,则现在将如下所示:

[
  {
    x: "whatever the value of x was passed into that method", 
    y: "whatever the value of y was passed into that method"
  }
]
_queue: [
  {x:1, y:1},
  {x:2, y:2},
  {x:3, y:3},
  ...
];
_queue: [
  {x:x, y:y}, // what you had just inserted
  {x:1, y:1},
  {x:2, y:2},
  {x:3, y:3},
  ...
];

为什么?如果没有看到完整的代码,我不知道它正在向数组中添加一个对象,因此如果数组以前是空的,它现在将如下所示:

[
  {
    x: "whatever the value of x was passed into that method", 
    y: "whatever the value of y was passed into that method"
  }
]
_queue: [
  {x:1, y:1},
  {x:2, y:2},
  {x:3, y:3},
  ...
];
_queue: [
  {x:x, y:y}, // what you had just inserted
  {x:1, y:1},
  {x:2, y:2},
  {x:3, y:3},
  ...
];
为什么?如果看不到整个代码,我就不知道如何将该元素插入数组的前面

这里,我们有一个名为
\u queue
的数组,我们在其中插入
{x:x,y:y}

因此,如果队列看起来像这样:

[
  {
    x: "whatever the value of x was passed into that method", 
    y: "whatever the value of y was passed into that method"
  }
]
_queue: [
  {x:1, y:1},
  {x:2, y:2},
  {x:3, y:3},
  ...
];
_queue: [
  {x:x, y:y}, // what you had just inserted
  {x:1, y:1},
  {x:2, y:2},
  {x:3, y:3},
  ...
];
现在看起来像这样:

[
  {
    x: "whatever the value of x was passed into that method", 
    y: "whatever the value of y was passed into that method"
  }
]
_queue: [
  {x:1, y:1},
  {x:2, y:2},
  {x:3, y:3},
  ...
];
_queue: [
  {x:x, y:y}, // what you had just inserted
  {x:1, y:1},
  {x:2, y:2},
  {x:3, y:3},
  ...
];
调用该函数的
insert
函数接受两个参数
x
y
,因此当我们插入一个对象时,如:

{x:x, y:y}
这意味着我们插入的对象的字段是:

{
  x: x, //(whatever argument was passed in for `x` when the function was called)
  y: y  //(whatever argument was passed in for `y` when the function was called)
}
只需将该元素插入数组的前面

这里,我们有一个名为
\u queue
的数组,我们在其中插入
{x:x,y:y}

因此,如果队列看起来像这样:

[
  {
    x: "whatever the value of x was passed into that method", 
    y: "whatever the value of y was passed into that method"
  }
]
_queue: [
  {x:1, y:1},
  {x:2, y:2},
  {x:3, y:3},
  ...
];
_queue: [
  {x:x, y:y}, // what you had just inserted
  {x:1, y:1},
  {x:2, y:2},
  {x:3, y:3},
  ...
];
现在看起来像这样:

[
  {
    x: "whatever the value of x was passed into that method", 
    y: "whatever the value of y was passed into that method"
  }
]
_queue: [
  {x:1, y:1},
  {x:2, y:2},
  {x:3, y:3},
  ...
];
_queue: [
  {x:x, y:y}, // what you had just inserted
  {x:1, y:1},
  {x:2, y:2},
  {x:3, y:3},
  ...
];
调用该函数的
insert
函数接受两个参数
x
y
,因此当我们插入一个对象时,如:

{x:x, y:y}
这意味着我们插入的对象的字段是:

{
  x: x, //(whatever argument was passed in for `x` when the function was called)
  y: y  //(whatever argument was passed in for `y` when the function was called)
}
当您第一次遇到它时,它的语法很奇怪,但在javascript中,键不能是变量,因此被视为文本,值就是变量

因此,如果
x=1

var o = {
  x:x
}
将意味着
o={x:1}

如果您确实希望使用变量键,那么可以使用

var x = 1;
var key = "x";
o = {}
o[key] = x;
当您第一次遇到它时,它的语法很奇怪,但在javascript中,键不能是变量,因此被视为文本,值就是变量

因此,如果
x=1

var o = {
  x:x
}
将意味着
o={x:1}

如果您确实希望使用变量键,那么可以使用

var x = 1;
var key = "x";
o = {}
o[key] = x;

_队列是x和y坐标的数组。e、 例如,
_队列=[{x:10,y:20},{x:11,y:30}]这有帮助吗?x和y是可变的,感谢澄清它取消了一个对象的移位,该对象有两个键x和y,其值是函数参数x和y的值-注意,ES2015(或2016,永远不确定)的缩写应该是
this.\u queue.unshift({x,y})\u队列是x和y坐标的数组。e、 例如,
_队列=[{x:10,y:20},{x:11,y:30}]这有帮助吗?x和y是可变的,感谢澄清它取消了一个对象的移位,该对象有两个键x和y,其值是函数参数x和y的值-注意,ES2015(或2016,永远不确定)的缩写应该是
this.\u queue.unshift({x,y})他说他知道要换什么does@KeithNicholas我知道这一点,但这并不意味着为了完整性,我不能快速回顾它所做的事情。我还更新了我的答案,以涵盖更多的信息。他说,他知道什么是取消换档does@KeithNicholas我知道这一点,但这并不意味着为了完整性,我不能快速回顾它所做的事情。我还更新了我的答案以涵盖更多信息