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


Richard Contreras
Richard Contreras
7,026 Points

Beginning constructor function question

Please see this codepen: https://codepen.io/pdxcoder/pen/wmZGam

I'm just starting to learn about constructor functions and I was goofing around. I wrote some code where you type values into the input fields, click submit, and those values are logged to the console. It works, but I'm wondering, how do I access these values afterward?

For example, I can type object.user1 in the console and get 'jack', per line 22 of the JS.

However, I was expecting, AFTER I entered values in the input fields and clicked submit, to be able to go into the console and type User.newGuy and see what I had typed. But I just get 'undefined'. How do I access, for at least the life of the page (ie, no server), the values in the field when I click submit?


1 Answer

james south
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

newguy is not accessible because it is scoped to the click handler on the button. initialize it in the global space then define it in the handler, then it will be available as newGuy, not User.newGuy.