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 trialPaul Son
5,474 PointsMethods and functions
What is the difference between methods and functions? When should you use them?
2 Answers
Tom Mertz
15,254 PointsMethods and Functions are pretty much the same thing. They should be used when you have a task that you want to be able to repeat, but not have to rewrite the code over and over again. Instead you would just call the method/function to handle the task. For example, if you need to calculate a value multiple times using the same expression.
A method is just a function that is associated with an object. So if you have a function that belongs to an object, it would be called a method of that object.
So, function - a block of code that can be repeated by calling it.
Method - a function that belongs to an object and describes some behavior of that object.
Good luck!
Dennis Castillo
16,018 PointsThe "FUNCTION" can be called directly when an event occurs (like when a user clicks a button), and it can be called from "anywhere" by JavaScript code.
A "METHOD" is just an object property that happens to be a function.
Paul Son
5,474 PointsPaul Son
5,474 PointsAwesome. Thanks!