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.

CSS jQuery Basics (2014) Creating a Password Confirmation Form Perform: Part 2

Andrew's Suggestion of Anonymous Function

Right at the end of the video Andrew suggests using an anonymous function to shorten some of the code for the event on password input. It is late, and I am tired, but I don't see how this will shorten it since these all have to be called at some point. Thoughts?

It seems it was just a suggestion as a possible method of shortening things up a bit since that particular line of code was so lengthy. It works either way, so I'd imagine it was just a suggestion if you preferred to try it out.

2 Answers

Here's how I think the code will be shortened:

Instead of the code looking like the line bellow:

$password.focus(passwordEvent).keyup(passwordEvent).focus(passwordEvent).keyup(confirmPassEvent);

It'll look like this:

$password.yourFunction();

I'm guessing that that your function will be returning the focus and keyUp method.

However how to write the function, I'm not sure. If someone knows how to return jQuery methods from a function that will be great to know.

Jake Hudson
Jake Hudson
7,841 Points

the part that he copied and pasted there at the end.

to keep it dry, instead of copying and pasting, you turn the repeated part into a short function (write it once) and then when you need it the second time, you just call it by its name.

that my take on it anyway.