What have I learned this week?
Before we talk about how to do this, we need to understand what Closures and IIFEs are.
Let's proceed in stages.
Taking advantage of this mechanism, Closures allow us to encapsulate variables external to the function declaration, to our function, in the execution context. In this way, each element of the execution context will possess its own variables.
IIFEs are unnamed functions that are executed as soon as they are declared. Their variables are visible only inside this function and this method allows us to create a sort of private variables.
The last step is to combine the two concepts just seen to obtain encapsulation and information hiding. That is, the possibility of declaring internal variables and functions that will not be accessible to the outside world, since they are private, and public functions that will instead be available to the entire project.
I also found some articles that go into more detail:
Understanding The Execution Context & Stack
The execution context has a variable called "this" that is bound to it.
The variable "this" in the global context is the window object.
Though, if we create an object, the "this" inside the object will refer to that specific object. We can for example create a single object to show that the object variable containing the name becomes accessible using this syntax.
I found this article that explains the problem well:
What if I have a function, external to an object and I want to use it for that object:
How can we change the value of "this"?
There are functions such as bind, call and apply that allow us to change the value of this.
The bind method allows also to fix the value of the arguments that will be taken by the new function that will be created.
The difference between bind and call/apply is that: bind returns a function without executing it, while call and apply execute it directly.
So what's the difference between call and apply?
The way they accept arguments.
Both functions take an object as first argument, but while the call method accepts a variable number of arguments separated by a comma, the apply method accepts as second argument an array composed of a variable number of elements.
I'll stop here for today, I'll edit this part later to include the other topics.
Anyway, here's the full log for the week:
Plenty of quizzes. I also improved my snake game and now the canvas is reponsive.
I have added a few functions to handle the swipe gesture and now my snake game also works on my phone !
I used a small hack to achieve this by using an overlay.
I could have achieved the same result by extending the entire canvas and reducing the playing area... ¯\_(ツ)_/¯
Today's topics are:
First-class function https://en.wikipedia.org/wiki/First-class_function…
Now obsolete because of modules? https://freecodecamp.org/news/how-to-use-es6-modules-and-why-theyre-important-a9b20b480773/
Today's topics are:
The 2nd part of the course is turning out to be better than the first!
Objects, the constructor function, Prototypes, the prototype chain, classes.
I think I'm going to die of exhaustion this week.
I must say that it was good to review some concepts that I already knew, but by taking a different course I was able to notice some nuances of the language that I had missed.
I wonder how many people like me do the challenge and work 8 to 10 hours a day.