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 trial

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.