Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Callback Functions in JavaScript!
You have completed Callback Functions in JavaScript!
Preview
In this video, we'll convert our function declaration into an anonymous function.
Anonymous Functions
An anonymous function is a function without a name.
function ( ) {
//Do something
}
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Another common way you'll encounter and
0:00
write callback functions
is as anonymous functions.
0:02
An anonymous function is
a function without a name.
0:05
In the case of a callback an anonymous
function can be written in place or
0:09
in line where the name of
the function would be.
0:14
Let's convert sayHello into
an anonymous function.
0:17
With practice, you can jump these
steps that I'm going to take.
0:21
But for now, I'll show my work.
0:25
First, I can change my sayHello
function from a function
0:28
declaration to a function
expression by adding a variable,
0:32
sayHello, And
deleting the name of the function.
0:37
Now to convert this function
to an anonymous function,
0:46
I can just get rid of the variable name.
0:50
How do we pass the anonymous function into
the executeCallback without a reference?
0:53
The function doesn't have a name anymore.
0:59
We can write it in place
of the sayHello name.
1:02
We can do that by cutting and
1:06
pasting the anonymous function
over the sayHello name.
1:11
When writing a function like this,
this is also know as inlining or
1:16
writing a function in line.
1:21
It can be more expressive.
1:23
You can clearly see a function
as a parameter here,
1:25
which isn't always obvious
when you use a named function.
1:29
You can see it's a callback, and
1:33
that the function could get executed
at any given point in the future.
1:35
A callback function is more obvious when
it's written as an anonymous function.
1:40
I've got a small challenge for
1:45
you, write another anonymous
function that will print goodbye.
1:47
Pause the video, and
then I'll show you how I did it.
1:52
I can just copy and paste the execute
callback and replace hello with goodbye.
1:57
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up