Okay, reader. If you have any of those questions, today is your lucky day. We are going to find out the answers for them.
How Does It Work?
To include JS files on a web page we would have to add
It is recommended that these blocks are right after the last HTML tag before the
The Domain Object Model (DOM)
By examining the previous code, we can see the use of the
In the case of the
setTimeout function, it takes two parameters: the first is a function that will execute after a specified amount of milliseconds; specified by the second parameter. The function we specified as the first parameter is what we know as an “anonymous function”. We call it “anonymous” because it has no name. Really? Yes, really. But you can assign a name to it if you want:
It now has a name. But we can still use it as a variable and pass it as first parameter of the
- Dude 2: “Damn, kids these days. Let’s just allow functions to contain variables and other functions. Voilà. We got classes.”
- Dude 1: “That sounds like is going to confuse lots of people… D:”
- Dude 2: “Meh. Not really.”
- Dude 1: “But…”
- Dude 2: “NOT REALLY, I SAID!”
Okay maybe it was not like that but, since then, we have like four or five different ways to define something like classes. The one I like the most is as follows:
How pretty does that looks? So, a function can also be a class definition with private members and all, just like any object-oriented language. This is known as prototyping.
this. Things are just starting to get more and more interesting for sure.
The “this” Keyword
If you have worked with classes before, surely you’ll recognize the
this keyword refers to the function owner and can refer to several things depending on the scope it is used:
window DOM element.
Now, you can see the last two usages in the table involve the use of the
apply() functions. These functions are useful if we would like to change the value this refers to. That would be your homework: Check the use of
Stay tuned! 😉