Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


Shon Levi
Shon Levi
6,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???

1 Answer

29,444 Points


.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.