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

Andrew Courey
Front End Web Development Techdegree Graduate 15,310 PointsCreating an object in Javascript with custom declaration
In Javascript, there are some objects like the Number object or the Array object that let you create them by typing a number like 5, or 10.6, or create arrays with [] instead of new Array(1,5,19). How can I create my own object where I can type $5 and it automatically creates a dollar object?

Andrew Courey
Front End Web Development Techdegree Graduate 15,310 PointsI am looking for a way to create an object, for example, a money object that has a literal declaration so I could type $10.21 and it would create a money object, instead of typing new Money(10.21);
1 Answer

Steven Parker
242,191 PointsWhat you're talking about would involve using symbols in ways that contradict the syntax rules of the language. So you would not be able to do this directly in JavaScript.
You might be able to do this by creating "transpiler" that would recognize your special syntax and translate it into JavaScript code. But it would be a bit tricky because some ordinary JavaScript identifiers might look like object creations in your new syntax.
ravindrareddy
10,590 Pointsravindrareddy
10,590 PointsWe can use an object literal in Javascript to create an object with the necessary properties and methods. Not sure if this is what you are expecting. Ex: var person = { firstName : "Stephen", lastName: "Curry", age: 31, country: "USA" };