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 trialorange sky
Front End Web Development Techdegree Student 4,945 PointscallTwice(myFuntion) really Anonymous?
Hello! 1) the lecturer said that callTwice(function(){..} is calling an anynomous function, but I think callTwice function is referring to the var callTwice = function(targetFunction), hence it is not calling an anymous function. Isn't callTwice(function(){..} calling a function that is stored in this variable:
var callTwice = function(targetFunction)
callTwice(function(){
console.log("Hello I am Peter");
undeclaredVariable;// Why does it print only once;
})
2) When I comment //'undeclaredVariable', Hello I am Peter"); prints 3times to the screen. but when I remove the comment from undeclaredVariable; Hello I am Peter prints only once to the screen. WHy is that?
the orginal code:
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<script>
/*var myFunction = function (){
console.log('myfunction was called');
undeclaredVariable;
}*/
var callTwice = function(targetFunction){
targetFunction();
targetFunction();
targetFunction();
}
callTwice(function(){
console.log("Hello I am Peter");
undeclaredVariable;
});
</script>
</body>
</html>
5 Answers
Hossam Khalifa
17,200 PointsYour question is a little bit complicated I will try to make it clear:
var myFunction = function (){
console.log('myfunction was called');
undeclaredVariable;}
is an anonymous function saved in a variable named myFunction.
var callTwice = function(targetFunction){ targetFunction(); targetFunction(); targetFunction(); }
This is an anonymous function saved in a variable named callTwice with parameter that takes a function called targetFunction. This function should execute the function passed in three times.
Both of the above functions are anonymous functions saved in a variable so we can call them using the variable name.
-
callTwice(function(){ console.log("Hello I am Peter"); undeclaredVariable; });
You used the variable name to call the function callTwice and passed in an anonymous function that can not be used again because it is anonymous and its not stored in variable. And it only prints one time because undeclaredVariable; is undeclared so it generates an error and stops execution.
Hope this was helpful
Hossam Khalifa
17,200 PointsmyFunction was not called so we can just ignore it now. But ``` var callTwice = function(targetFunction){ targetFunction(); targetFunction(); targetFunction(); }
should call a specified function 3 times.
But when you called an Anonymous function with callTwice
it printed the first time but when it reached undeclaredVariable; (wich is undeclared)
it generated an error. So it did not continuo the callTwice function and it stopped. So the output is only the first console.log.
Hossam Khalifa
17,200 PointsYour Welcome :)
orange sky
Front End Web Development Techdegree Student 4,945 PointsHello Hossam,
I think I am lost Js vocabulary
1) Is "var callTwice" calling " callTwice( function(){....} "or 2) Is "callTwice (function(){console.log(...); undeclared;} "calling "var callTwice= function(){....}" 3) which is the anynomous function "var callTwice=function()..." or "callTwice (function(){...}"
Thanks!!!
orange sky
Front End Web Development Techdegree Student 4,945 PointsThank Hossam!! Your explanation was really clear. :)