---
# System prepended metadata

title: JS 常遇到的問題

---

# JS 常遇到的問題

## 座標相加減
|  | c++ | js |
| -------- | -------- | -------- |
| 座標相加     | Vec(1,1) + Vec(2,2)     | cc.pAdd(cc.p(1,1), cc.p(2,2))     |
| 座標相減     | Vec(1,1) - Vec(2,2)     | cc.pSub(cc.p(1,1), cc.p(2,2))     |

## mutable variable is accessible from closure
以下code會出現此error:
```javascript=
var that = this;
for (var r = column; r <= column + length - 1; r++) {
    var sym = this.getSymbolAtPosition(r, row);
    if (sym) {
        sym.runAction(cc.sequence(cc.fadeOut(1), cc.callFunc(function() {
            sym.turnWildImage(r < 2 ? that.WILD_BOY_SYMBOL_INDEX : that.WILD_GIRL_SYMBOL_INDEX);
        })));
    }
}
```

Solution 1:
```javascript=
var that = this;
for (var r = column; r <= column + length - 1; r++) {
    var sym = this.getSymbolAtPosition(r, row);
    if (sym) {
        (function (sym, r) {
            sym.runAction(cc.sequence(cc.fadeOut(1), cc.callFunc(function() {
                sym.turnWildImage(r < 2 ? that.WILD_BOY_SYMBOL_INDEX : that.WILD_GIRL_SYMBOL_INDEX);
            })));
        })(sym, r);
    }
}
```

Solution 2:
```javascript=
var that = this;
for (var r = column; r <= column + length - 1; r++) {
    var sym = this.getSymbolAtPosition(r, row);
    if (sym) {
        sym.runAction(cc.sequence(cc.fadeOut(1), cc.callFunc(function(target, data) {
            data["sym"].turnWildImage(data["r"] < 2 ? that.WILD_BOY_SYMBOL_INDEX : that.WILD_GIRL_SYMBOL_INDEX);
        }, {"sym": sym, "r": r})));
    }
}
```