set(x, y, element) {
var coord;
if(typeof arguments[0] === "object") {
[coord, element] = arguments;
({x, y} = coord); // hmm Oo
}
this.grid[x][y] = element;
}
get(x, y) {
if(typeof arguments[0] === "object") {
var {x, y} = arguments[0]; // no wlasnie..
}
return this.grid[x][y];
}
Kod testowy
let coord = {
x: 10, y: 10
};
this.grid.set(coord, "coord");
console.log(this.grid.get(10, 10));
this.grid.set(15, 15, "normal");
console.log(this.grid.get(15, 15));
Pierwsze z arrayem jak najbardziej działa bez podania var, bo element jest zadeklarowany jako parametr, a coord kilka linijek wyżej.
Zgodznie z tym co piszą na mozilli:
The ( .. ) around the assignment statement is required syntax when using object literal destructuring assignment without a declaration.
Nie powinno być konieczne użycie nawiasów.. a jednak. ({x, y} = coord);
- wtf przecież x, y też są zadeklarowane jako parametry (tak samo jak element). Wciskanie var'a tam odpada bo biorąc pod uwagę hoisting to jak nie podam obiektu to otrzymam 2x undefined. W takim razie co robią te nawiasy i dlaczego są konieczne?
Dałem tam console.log i wypluł 2x undefined (x i y), a dla foo Uncaught ReferenceError: foo is not defined
więc wychodzi na to, że są zadeklarowane, ale w tej linijce var {x, y} = arguments[0]; // no wlasnie..
js juz o tym zapomina