Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Foundations Functions Anonymous Functions

Anonymous Function

I am pretty lost on this anonymous Function video lesson. Pretty much the most difficult lesson which confused me and now I am stuck with this part 2 of the question. Can anyone please share some knowledge on this top and the question? I will really appreciate it.

index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>JavaScript Foundations: Functions</title>
    <style>
      html {
        background: #FAFAFA;
        font-family: sans-serif;
      }
    </style>
  </head>
  <body>
    <h1>JavaScript Foundations</h1>
    <h2>Functions: Anonymous Functions</h2>
    <script>
      var anonymousFunction = function (){};

      var Newfucntion= (function() {window.didExecute=true;})

      (function () {
        window.didExecute = true;
      });
    </script>
  </body>
</html>

5 Answers

Hi layeesolo,

It looks like you've added a bit extra to the challenge by implementing another function called "Newfucntion". This is not necessary.

The second function that sets "window.didExecute = true" only needs to be called. The parenthesis surrounding (function () { ..... }) evaluates the function we have, but it must still be called like a normal function. And if you remember how we call functions, we put the name of the function and then (). So, if we have a function named callMe and we want to call it, we put:

//Execute the function callMe()
callMe();

So, given that it takes those extra () at the end to execute the function, that is simply what we need to add to the end of the second anonymous function in order to call it:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>JavaScript Foundations: Functions</title>
    <style>
      html {
        background: #FAFAFA;
        font-family: sans-serif;
      }
    </style>
  </head>
  <body>
    <h1>JavaScript Foundations</h1>
    <h2>Functions: Anonymous Functions</h2>
    <script>
      var anonymousFunction = function (){};

  //Added () to the end of the anonymous function to call it 
      (function () {
        window.didExecute = true;
      })();
    </script>
  </body>
</html>

I hope that clears it up!

Hugo Paz
Hugo Paz
15,622 Points

Hi layeesolo

You need to add parentheses at the end of the anonymous function, like so:

(function () {
        window.didExecute = true;
      })()

thank this javascript foundation confuses me compared to the basic or reading a book.

There is an updated course given by Dave McFarland called Javascript Basics. You might find that to be easier, layeesolo. :)

i will once i am done with this course. I am on the font end track and this is a requirement for the course.

Ah yes, I forgot about that. Good deal! If you ever need any help, you can tag me on here with the @ symbol and my name or shoot me an email at marcus.parsons@gmail.com. :)

alright, I will and thank for your help. I appreciate it

My pleasure.