0

2 つの引数を変更してオブジェクトを返す関数があります。

...
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;

確かにうまくいきます。質問は、オブジェクトの代入を分解する際に「this」を使用できるか、およびそれを適切に使用する方法です。

4

0 に答える 0