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
tayjohn
Courses Plus Student 1,595 PointsPassing an anonymous function as parameter in javascript
I am a bit uncertain regarding what actually occurs if you pass an anonymous function as a parameter when calling another function.
For instance I have the following:
getAllDishes = function (type, filter, cb) {
...
cb(dishes);
}
And then I call the function with the 3 parameters and one being an anonymous function:
getAllDishes(type, filter, function(dishes){
//some code here..
dishes; //does this contain the value from cb(dishes) above?
});
Does this mean that when I call getAllDishes, the "dishes" value in the parameter "function(dishes)" will be the dishes value in "cb(dishes)" ?
1 Answer
Steven Parker
243,318 PointsYou seem to have the right idea.
Perhaps you're uncertain because of the re-use of the name "dishes"? The name of the parameter of the anonymous function is not important, only that the parameter was passed when it was invoked.