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!
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
Shon Levi6,036 Points
Code work only without () - why?
Hey, I understand everything on the stage of making the checking and prop, keyup, focus, etc...
but when I tried to make that code - nothing worked $("#password").focus(passwordEvent()).keyup(passwordEvent()).keyup(confirmPasswordEvent()).keyup(enableSubmitEvent());
that - worked: $("#password").focus(passwordEvent).keyup(passwordEvent).keyup(confirmPasswordEvent).keyup(enableSubmitEvent);
(the changes is to add () in the end of calling the functions)
Why is not working with the () - what if I have some function that need arguments??? if I can't use parentheses how it will work???
.focus method expects a function. But if you pass passwordEvent() you're not passing the function. As you said "()" runs the function so, after it gets executed it passes returned value to the focus method. And focus can't work with that. So yes, in that case you should use only function name, as you want to pass whole function, not it return value.
If you need to pass argument there is a trick:
$("#password").focus(passwordEvent.bind(null, argument1, argument2)) // etc
Bind creates a new function with those arguments but does not execute it. So focus gets the function it needs with arguments provided. There is no need to change context of this so the first argument is left as null.