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

JavaScript jQuery Basics (2014) Creating a Password Confirmation Form Perform: Part 1

Rafael Marcano
Rafael Marcano
3,735 Points

Did not understand the named method part (passwordEvent).

I feel there was a sudden jump in assumed knowledge when using the named method.

How does this "named method" work?

What do I need to read to understand? In the JS track the OOP part is still a couple of modules away.

2 Answers

Amber Lim
Amber Lim
4,707 Points

Hi Rafael. I hope this helps:

Dave Mcfarland's course Javascript Basics covers this.

To keep your code DRY, you can name a function, so that you can use it over and over in other parts of the code just by calling that function.

So for a simple example,

var word = prompt("Find the amount of letters in a word"); 

function printLengthOfWord() { //name function
  console.log(word.length);
}

printLengthOfWord(); //call the function as many times as you want
printLengthOfWord();
printLengthOfWord();
printLengthOfWord();

This is the same idea with what Andrew talked about. The reason the function passwordEvent was passed into the parentheses of .focus() and .keyup() is because A FUNCTION NORMALLY GOES BETWEEN THE PARENTHESES OF THESE JQUERY EVENTS. So, if you named your function, there is no need for you to retype the WHOLE function code again when you could just call it by its name (which in this case Andrew named his function passwordEvent.)

Let me know if this didn't help :)

Hi Rafael, I'm not sure exactly what you are having problems with but, basically, Andy is creating a reusable snippet of code to remove repeated functions.

In this case, Andy was using the same bit of code on either a focus() or keyup() event so to keep from writing "Wet DRY" (code that is repeated multiple times throughout your application). He created a function (name method) to achieve the same result without having to repeat the same block of code more than once.

Please let me know if you have a more specific question.