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 trialRhett Herring
Courses Plus Student 11,023 PointsWhy do we call the functions with no () ?
Not sure why in the following line
$password.focus(passwordEvent).keyup(passwordEvent).keyup(confirmPasswordEvent).keyup(enableSubmitEvent);
We don't have to call passwordEvent and the other functions to include the '()' like passwordEvent() ???
2 Answers
Thomas Nilsen
14,957 PointsHere is an additional example to help you understand:
//We add a new function to the Array prototype.
//This basically means you can call this function on arrays.
//Our 'callback' parameter is a function
Array.prototype.customMap = function(callback) {
var arr = [];
//'this' - is whoever called the function. i.e. some array
for (var i = 0; i < this.length; i++) {
//Here we actually call the 'callback' function
//and pass in a number from the array
arr.push(
callback(this[i])
);
}
return arr;
}
//This is a function we can pass to our customMap function
function multiply(n) {
return n * 2;
}
var arr = [1, 2, 3, 4];
//Notice - We're not calling the 'multiply' function here -
//Just passing it in so our customMap function can call it at some point
console.log(arr.customMap(multiply));
Michael Hulet
47,913 PointsIn this case, those functions where you don't write the parentheses afterwards are just acting like normal variables. You're not calling them, but you're passing them into those other functions, so those functions (the ones with the parentheses) can potentially modify and call the functions (the ones without the parentheses) you gave them
john larson
16,594 PointsMichael, that's a good answer, thank you. Now, how will we identify in the future, when we will be using functions as "normal" variables. (Thomas showed a code example but I couldn't relate it to this instance)
Michael Hulet
47,913 PointsYou do that commonly in cases where you want to have another function
execute some code after a certain thing has happened, for example when a download finishes. Let's say you have a function
that gets data from a server. You don't want to prevent all the other JavaScript from running, so this function
will return
immediately so the rest of your code can keep running, but it'll call a function
you give it when the download finishes. You can write another function
that you'll give to the function
to download data, like this:
function getData(callback){
var data = /* All the code it takes to get data */
callback(newData);
}
function finishedGettingData(newData){
//Idk update your UI or something with the new data
}
getData(finishedGettingData);
In the case above, finishedGettingData
isn't called until getData
is done downloading all of its data, but getData
knows to call finishedGettingData
in specific, because that's what you told it to call
john larson
16,594 PointsThanks Michael.
Rushabha Jain
Courses Plus Student 132 PointsRushabha Jain
Courses Plus Student 132 PointsBut for execution of the program we need to give parenthesis after the name of the function. which is not given in -------------------------------------------------------- $password.focus(passwordEvent).keyup(passwordEvent).keyup(confirmPasswordEvent).keyup(enableSubmitEvent)