To better help you understand it, I'll structure the code given with pseudo code
OBJECT abc IS A FUNCTION THAT DOES
RETURN ANONYMOUS OBJECT
LOG TO CONSOLE this.val
Let's examine the function declaration that you are most familiar with.
function myFunction() {
//do stuff
}
In the example given, you are being shown anonymous functions. In the example above, the function is declared with a name, which is myFunction
. But with anonymous functions, no name has to be given when declaring. So in basis, the function has no name, making it anonymous.
The point of a function is to avoid repetitive code. So giving functions a name allows you to call them from anywhere. With anonymous functions, you cannot call them unless assigning them to a variable.
Anonymous functions are used when usually grouping code. Whether the group of code is for an event listener
or for other things.
You can learn more about anonymous functions here.
Also in this example is anonymous objects. You are probably familiar with giving objects names. But what return
is returning is an anonymous object. Within the anonymous object is what we're about to look at.
An object structure looks similar to this:
objectName
key1: value1
key2: value2
So in this case, the anonymous object that return
is returning has a key called log
. The value for this key is an anonymous function that logs this.val
to a console.
You can learn more about objects here.
Happy Coding,
Farouk