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 Object-Oriented JavaScript (2015) Constructor Functions and Prototypes Constructors Review

Prakhar Patwa
Prakhar Patwa
11,260 Points

i dont understand , what they want to ask?

Given the following code.

function Animal(breed, noise) { this.breed = breed; this.noise = noise; this.makeNoise = function() { console.log(this.noise + ", " + this.noise); } }

What is the correct way to create an instance of an Animal?

2 Answers

Jason Romero
Jason Romero
20,917 Points

They want you to choose which is the proper way to create a new Animal object. For example... var dog = new Animal("Dalmation","Woof");

Sean Flanagan
Sean Flanagan
33,235 Points

Hi Prakhar Patwa. Have a look at each answer.

For example,

var dog = Animal("Dalmatian", "Woof");

Look carefully. This is wrong. What's missing from this? What keyword is used to create instances of a constructor function?

Next,

var cat = new Cat("Siamese", "Meow");

Look at the difference between the name of the original constructor function and the name of the constructor function used to create an instance. Do you notice anything? They are not the same. So that's wrong.

Next,

var frog = new animal("Tree Frog", "Ribbit");

Look at the first letter of animal. The name of a constructor function always starts with a capital letter, not a lower-case one. So this is wrong.

You're left with:

var horse = new Animal("Shetland pony", "Neigh");

Here, the new keyword is included, the right constructor function name is used, and its first letter is capitalised, so this is the right answer.

I hope this helps.