JavaScript30 Day #01 – ReactJS, this, methods that mutate an Array.
This post is over a year old! Information may be stale. Please read with caution.
What I did today
ReactJS: Tic-Tac-Toe.
Followed ReactJS’ tutorial from the official website to build a Tic-Tac-Toe game.
I had previously been asked to build this game as an interview exercise and it’s eye-opening how simple the approach could have been versus the route I had taken in the exercise. Granted the interview exercise included an additional requirement for the board to be dynamic, not fixed to 3 by 3.
I’ll attempt the suggested exercises tomorrow and see how I fare.
Today, I did see a couple of interesting tricks, though:
- Create an array of N length with each value preset to a known value:
Array(9).fill(null) - Cloning an array:
exampleArray.slice() - Adding to an array:
exampleArray.concat([{ foo: "bar" }])(versus what I’ve been using:newArray = [...exampleArray, { foo: "bar" }])
There’s nothing magical about these snippets, just something that becomes second nature the more you use them. 😊
JavaScript: this
Studied briefly this in functions vs arrow functions:
A function’s
thiskeyword behaves a little differently in JavaScript compared to other languages. It also has some differences between strict mode and non-strict mode.In most cases, the value ofthisis determined by how a function is called (runtime binding). It can’t be set by assignment during execution, and it may be different each time the function is called. ES5 introduced thebind()method to set the value of a function’sthisregardless of how it’s called, and ES2015 introduced arrow functions which don’t provide their ownthisbinding (it retains thethisvalue of the enclosing lexical context).
Arrow functions do not default
thisto the window scope, rather they execute in the scope they are created:
New article
While this was technically done yesterday… worked on an article explaining the Singleton design pattern; not yet ready for publishing.
Resources
- To Mutate, Or Not To Mutate? JavaScript Array Methods Cheat Sheet
I chanced upon Annie’s DEV.to blog while wondering if there’s a trick to remembering which Array methods mutate the original array, and which ones don’t.