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 trialRobin Dykema
10,026 PointsIf passwordEvent is a function, why doesn't it need () after it?
If passwordEvent is a function, why doesn't it need () after it?
For example, $("#password").focus(passwordEvent).keyup(passwordEvent); is correct but $("#password").focus(passwordEvent()).keyup(passwordEvent()); is incorrect.
Why?
3 Answers
Michalis Efstathiou
Courses Plus Student 4,638 Pointsthe focus function is aware that you are passing in another function even though you dont include the () and will bind it to the event correctly
as I said, if you include the () you are calling the function at the exact point that the code is read, and at that point you do not want to execute it, you only want to bind it
Michalis Efstathiou
Courses Plus Student 4,638 Pointsconsider this code
var focus = function ( functionName ) {
functionName();
};
focus(someFunction);
so you see, here we can call the focus function, pass it the name of another function without the brackets, and it will still execute the function
Michalis Efstathiou
Courses Plus Student 4,638 Pointsbecause when you add () to a function name, you call the function. so when you run the code the function will run, but you dont want that, you only want the function to be binded to those events
Robin Dykema
10,026 PointsDon't I want to run it when the other events occur? When the focus() function occurs, don't I want my function to occur?