Javascript 是否可以对“this”字段使用解构赋值?
我有一个函数,可以更改两个参数并返回objectJavascript 是否可以对“this”字段使用解构赋值?,javascript,object,this,variable-assignment,destructuring,Javascript,Object,This,Variable Assignment,Destructuring,我有一个函数,可以更改两个参数并返回object ... pos2pix = function (x, y) { return { xpx: x * cellBase * 1.5 + offsetX + x * paddingX, ypx: 2 * S3D2N * y + (x % 2 === 0 ? S_AD : 0) + offsetY + y * paddingY }; }, ... 以及一个使用此数据的函数: ... Cell = fun
...
pos2pix = function (x, y) {
return {
xpx: x * cellBase * 1.5 + offsetX + x * paddingX,
ypx: 2 * S3D2N * y + (x % 2 === 0 ? S_AD : 0) + offsetY + y * paddingY
};
},
...
以及一个使用此数据的函数:
...
Cell = function (x, y, ctx) {
this.x = x;
this.y = y;
this.highlighted = false;
this.xpx = 0;
this.ypx = 0;
//error here
{xpx: this.xpx, ypx: this.ypx} = pos2pix(x, y);
this.ctx = ctx;
//find neighbours
this.nbhs = findNbhs(x, y);
};
...
我想要的是使用解构赋值。我尝试了以下尝试:
pos2pix = function (x, y) {
return [
x * cellBase * 1.5 + offsetX + x * paddingX,
2 * S3D2N * y + (x % 2 === 0 ? S_AD : 0) + offsetY + y * paddingY
];
},
...
this.xpx = 0;
this.ypx = 0;
[this.xpx, this.ypx] = pos2pix(x, y);
这个很好用。也
var coords = pos2pix(x, y);
this.x = x;
this.y = y;
this.highlighted = false;
this.xpx = coords.xpx;
this.ypx = coords.ypx;
当然可以。问题是,在用对象构造赋值时是否可以使用它,以及如何正确使用它。不适用于JS,请考虑:var tmp=pos2pix。。。;this.xpx=tmp.xpx。。。等等…当你说这个很好用的时候,你真的检查过了吗?我发现以下错误:未捕获引用错误:赋值中的左侧无效。为什么不@几年内可能达到的标准普尔亿意味着2016年。我希望文明在那个时候还活着。