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
paulscanlon
Courses Plus Student 26,735 PointsObject oriented JavaScript
Hi all
Still trying to wrap my head around things like constructor functions etc.
These two calculators work exactly the same. What are the pros/cons of using either one?
Thanks
Paul
1 Answer
Steven Parker
243,656 PointsWell, "calcZero" is an object literal. It's a single instance of an anonymous object. But "calcOne" is a constructor function. When called with the "new" operator it will create a new "calcOne" object instance.
Also, "calcZero" is a plain object but "calcInstance" is a class object that contains a prototype that can be used to establish an inheritance chain.
If you only need one object of this type, and are not going to extend its properties to other classes, the anonymous object literal is probably a good choice. But if you have a need for more instances and/or derived classes, then the class constructor would be the way to go.
paulscanlon
Courses Plus Student 26,735 Pointspaulscanlon
Courses Plus Student 26,735 PointsThanks a lot, it's one thing copying what i've seen in a Treehouse video, but it's more difficult trying to understand why i'm doing these things. Cheers Paul