Queenscripts / flowerField

My first game hard coded in vanilla javascript.
1 stars 0 forks source link

Feedback! #1

Open cferdinandi opened 5 years ago

cferdinandi commented 5 years ago

Hey there, @ChrisFerdinandi from Twitter here!

So, this is super awesome, and honestly, I have almost no feedback for you. This is really, REALLY well done!

A couple of small things for you.

Onload Event

Because you're loading your script in the footer, this isn't really needed:

window.onload = init;

You can run the init() event immediately, since all of the DOM it needs was loaded before it. This is only really needed when you're loading scripts in the header, which you generally don't want to do anyways.

Caching Elements

In a few places, you grab elements inside of functions or event listener callbacks:

displayMiss: function(location) {
    var cell = document.getElementById(location);
    cell.setAttribute("class", "miss");
}

That means the browser has to relook for those elements every time the function runs.

You can pull those out of the functions, grab them once when the script loads, and use them forever, since the DOM doesn't change at all in your app.

var cell = document.getElementById(location);

displayMiss: function(location) {
    cell.setAttribute("class", "miss");
}

Revealing Module Pattern

Your project seems like the perfect use case for the Revealing Module Pattern, which keeps some code priviate and lets some functions get used publicly, while keeping the whole thing out of the global scope.

It's a bit less buggy and more neat than the "functions in objects" approach you used, but this is a highly opinionated perspective so keep doing whatever works for you!

Here's an article I wrote on the pattern if you wanted to learn more.

Final note

I really had to struggle to find good feedback for this. Really nice work!

Queenscripts commented 5 years ago

Hey, thanks @cferdinandi! I really appreciate this feedback and will consider this in my next iteration. A couple of bugs to be fixed are the responsiveness and verbosity!