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

JavaScript JavaScript Foundations Objects Prototypes: Part 2

Jon Edwards
Jon Edwards
7,913 Points

Can't get Prototypes to work in Javascript Console

I have copied the code exactly as far as I can tell for the Prototypes Lesson: http://teamtreehouse.com/library/javascript-foundations/objects/prototypes-part-2

When I call the function jim.greet() in the Javascript console in Chrome, I always get the error " Type Error : undefined is not a function" and can't get the greet function to work, or the species (returns undefined), add a new name (Nick) etc. Also, it doesn't display quite the same way in the console, when I type jim, instead of showing a gray dropdown arrow, it just shows <- Person {name: "Jim"} Please help, Thanks!

Here is my code

http://codepen.io/anon/pen/yzlkJ

var personPrototype = {
    name: 'Anonymous',
    greet: function (name, mood) {
        name = name || "You";
        mood = mood || "good";

        console.log("Hello, " + name +
                    " I am " + this.name + 
                    " and I am in a " + mood + " mood!");

    },
    species: 'homo sapien'
};

function Person (name) {
    this.name = name;
}

Person.protoype = personPrototype;

jim = new Person("Jim");```

4 Answers

I've tried to answer, but as I switch back and forth between codepen and here I lose everything I write. So forgive me if I barely answer, but I'm looking!

  1. the backtick (```) is what you need for code snippets. It is to the left of numeral 1, with ~ as the shift option.
  2. It looked like you had an errant comma after a closing curly bracket ( }). Line 28 I think??

Trying to help, but pretty new myself, obviously. Good luck !!

Duh, it was line 11, and again, I am just starting to answer people so forgive me!

Jon Edwards
Jon Edwards
7,913 Points

Hmm thanks, I was able to post the code here now, but that returns Uncaught SyntaxError: Unexpected identifier

That comma is needed to separate the keys/values of the prototype object I believe.

Okay, you also have a space before the opening ( when beginning your function...should be function(blah, blah) {}

I think that might be it.

Jon Edwards
Jon Edwards
7,913 Points

I dont think its that space. Does anyone think this could be a problem with where the script is placed in the index.html? I am pretty sure I copied the code exactly. I thought this exercise could be done in a page with no html really since it's all just console. Thanks.

Jon Edwards
Jon Edwards
7,913 Points

After realizing that Jim misspelled his prototype variable in the video, I realized I forgot that extra "t" as well.

Person.protoype = personPrototype;

Thanks for helping!

Nice catch!