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 trialcyber voyager
8,859 PointsDifference between regular functions and constructor functions
How can I differentiate normal functions vs constructor functions?
2 Answers
Steven Parker
231,269 PointsIt won't always be obvious except by usage.
The best evidence of a constructor function is when you see it being used to create an instance with the "new" keyword. Other than that, there's no certain difference, but here's some things that might be clues:
- by convention, constructor function names start with capital letters
- inside a constructor function, you may see properties being set using "this"
- you may also see methods being created using "this" and anonymous functions
- a constructor wont return anything
Greg Schudel
4,090 PointsGreat, thank you.
cyber voyager
8,859 Pointscyber voyager
8,859 PointsThanks Steven, as always you are here to help.
Greg Schudel
4,090 PointsGreg Schudel
4,090 Pointsif it doesn't return anything how can you use it? I thought the whole point was being able to make a variety of similar instances of the object it's calling. Kinda like a factory drone on a conveyer line.
Steven Parker
231,269 PointsSteven Parker
231,269 PointsThe constructor function doesn't return anything, but the new keyword returns the new object.
Vishesh Singh
4,332 PointsVishesh Singh
4,332 PointsPlease check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new