Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

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?

Thanks.

1 Answer

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,258 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.