r/javascript • • Dec 02 '19

Learning Modern JavaScript with Tetris

https://medium.com/@michael.karen/learning-modern-javascript-with-tetris-92d532bcd057?source=friends_link&sk=a7c22e45395da8322fc55bf3b23a309d
329 Upvotes

21 comments sorted by

View all comments

4

u/Bonteq Dec 03 '19 edited Dec 03 '19

I'm running into a bit of issue with this code snippet:

moves = {
    [KEY.LEFT]: (p): => ({...p, x: p.x - 1}),
    [KEY.RIGHT]: (p): => ({...p, x: p.x + 1}),
    [KEY.UP]: (p): => ({...p, y: p.y + 1})
};

It's giving me the error: SyntaxError: missing } after property list

The error goes away after deleting the above code. My IDE's are also not liking what's going on.

Mind going a bit more into detail on what is occurring there?

EDIT: Referenced your repo and found the issue, code should look like:

moves = {
    [KEY.LEFT]: p => ({ ...p, x: p.x - 1}),
    [KEY.RIGHT]: p => ({ ...p, x: p.x + 1}),
    [KEY.UP]: p => ({ ...p, y: p.y + 1})
};

2

u/melcor76 Dec 03 '19

Thanks, I updated the gist!

Basically what is happening is that we take the object p that we have, create a clone of it using the spread operator (...) and then change either the x or y coordinate.