Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Foundations Functions Anonymous Functions

Don Shipley
Don Shipley
19,488 Points

JavaScript

No matter what I try it will not pass http://teamtreehouse.com/library/javascript-foundations/functions/anonymous-functions http://teamtreehouse.com/library/anonymous-functions On about line 18, make the anonymous function with the code 'window.didExecute = true' execute.

  var anonymousFunction = function(){};
// line 18 below
      (function () {
        window.didExecute = true;
      });

3 Answers

While it may look strange at first, you execute anonymous functions the same way you execute any other function, by adding the parentheses at the end of the function. When it comes to named functions, you add the parentheses after the function name to execute it like this:

someFunction();

An anonymous function obviously doesn't have a name, so you add the parentheses after the function body, like this:

(function () {
   // some code
})();

This is something called IIFE — Immediately-invoked function expression.

Don Shipley
Don Shipley
19,488 Points

var anonymousFunction = function () {};

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

Oops! It looks like Task 1 is no longer passing.

Look at my code again. There's only one semi-colon at the end of the function expression. You have an additional one in-between the parentheses.

Don Shipley
Don Shipley
19,488 Points

Thank you Dino... Tried that once before removing the ; gave me the same result. Maybe I had removed the wrong on but think I removed the same one before It passed this time..