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 triallayeesolo
7,316 PointsAnonymous 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.
<!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
Marcus Parsons
15,719 PointsHi 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
15,622 PointsHi layeesolo
You need to add parentheses at the end of the anonymous function, like so:
(function () {
window.didExecute = true;
})()
layeesolo
7,316 Pointsthank this javascript foundation confuses me compared to the basic or reading a book.
Marcus Parsons
15,719 PointsThere is an updated course given by Dave McFarland called Javascript Basics. You might find that to be easier, layeesolo. :)
layeesolo
7,316 Pointsi will once i am done with this course. I am on the font end track and this is a requirement for the course.
Marcus Parsons
15,719 PointsAh 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. :)
layeesolo
7,316 Pointsalright, I will and thank for your help. I appreciate it
Marcus Parsons
15,719 PointsMy pleasure.