Wikipeida on first-class functions:
In computer science, a programming language is said to support first-class functions (or function literal) if it treats functions as first-class objects. Specifically, this means that the language supports constructing new functions during the execution of a program, storing them in data structures, passing them as arguments to other functions, and returning them as the values of other functions.
If you ran this nothing would happen because it's not been invoked. Remeber function statments don't return a value until they are exectuted. If we were to invoke this we would be creating an object which has some properties one of which will be the 'hello' function. So rather than calling the hello function, which will just console.log 'Howdie', lets log the function itself.
If you run this in a browser you will see the function itself being returned. Now remeber that functions are objects which have properties. So lets see one of those properties.
Lets add another property
To really prove that functions are objects lets use Node.Js to show the object itself. Change the file to just console.log(hello). Run your file using
What you will now see in the console is the object itself
Or as Nicholas Zakas put it in his fantastic book:
"This doesn't throw an error because the function declaration is read first before the code begins to exectue."
You've most likely seen function expressions as well. An expression is a unit of code that returns a value.
A function expression is a block of code that results in a value.
This will cause an error
Finally lets pass a function as a variable.
Here the variable func is going to be a function. So the parameter we pass to logText will be a function itself, we can do this because functions are first-class. We then invoke the function within the logText function. So lets give this a go.
And I'm sure you know what we are going to get back here.
So lets summarise first-class functions:
are an instance of the Object type
have propeties and a link back to its constructor method
can be stored in a variable
can be passed as a parameter to another function