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

How the following challenge is not a constructor function?

I am taking the quiz and it asks that what sort of function it is, i checked "constructor" but it said wrong, how can i tell if it is a instance or constructor, isn't instances are creating from constructor by using new keywords?

4 Answers

Steven Parker
Steven Parker
229,982 Points

One question does have "constructor function" as the correct answer.

So I'm guessing you're asking about the other one that does not. If you compare those two questions, the example code is very similar in structure. Both end where a variable is assigned with a class instance created by calling a constructor. prefaced by the "new" keyword.

But in one question, you are asked to describe the function being called (which begins with a capital letter), and the correct answer is indeed a constructor function.

In the other question, you are asked to describe the variable being assigned (which begins with a lower-case letter), and that variable represents the class instance that is being created.

Here's a generic example:

//             this is the constructor function
//             vvv
var spot = new Dog("Dalmation");
//  ^^^^
//  this is a class instance
Mike Ngu
Mike Ngu
8,229 Points

Hi there,

This a good question :) Here's an example of an instance and a constructor function. Maybe it will clear some things up

var Person = function(name, occupation) {     //This is the constructor function. By convention the var is capitalize, but it does not have to be for it to work. 
     this.name = name;
     this.occupation = occupation;
}

var mark = new Person("Mark", "Student"); 

The var mark is the instance. It is an instance because the new keyword creates a new object, which allows the constructor function to point to the newly created object here. Hence, it's an instance of the constructor function. The instance is what guide the constructor function, so it will know to point to that object. It can definitely be confusing, but let me know if this make sense.

Mike Ngu i under that, but in quiz, they ask same question for instances and constructor and want to answer what is it, a constructor or instance, whereas both are the same.

Steven Parker
Steven Parker
229,982 Points

Look closer, as I said in my answer above those questions are not quite the same.

Steven Parker if you linked the pages, the link gone.

Steven Parker
Steven Parker
229,982 Points

I don't know any way to link to a specific question in a quiz. But if you check my answer above, I did my best to describe the difference between the two similar-appearing questions.

Mike Ngu
Mike Ngu
8,229 Points

Correct me if I am wrong, but I think this is the quiz question.

Given the following code what is the best way to describe task?

function TodoListItem(description) {     //This is the constructor function
  this.description = description;
  this.isDone = false;
  this.toString = function() {
    return this.description;
  }
  this.markAsComplete = function() {
    this.isDone = true;
  }
}

var task = new TodoListItem("Do the laundry!");     //This is not the constructor function but the instance 

If you compare this to my example above it should show why the var task is not a constructor function. Notice how the new keyword is creating a new object, then the TodoListItem constructor function is invoked. You can tell the difference by looking at each specific part and what it is doing :) Think of the constructor function as the blueprint, and the instance is what we put inside the blueprint. Hope this clear things up, but I can try to break it down another way if needed.