Comparing the <instance>.constructor property to the constructor instead of a string

In this exercise, we are checking if the instance created by the Dog constructor has a property named constructor by comparing it to an object so does it mean the the value of the candidate.constructor is a link to the constructor or am i missing something here and if it is a link instead of an the name of the constructor object, how does the comparison even work.

**Your code so far**

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

// Only change code below this line
function joinDogFraternity(candidate) {
if(candidate.constructor === Dog){
  return true
}
return false
}
  **Your browser information:**

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0

Challenge: Understand the Constructor Property

Link to the challenge:

In this exercise, we are checking if the instance created by the Dog constructor has a property named constructor by comparing it to an object

It does not check if it has a constructor, not technically. It also compares it to a function, not an object (except that in JS, functions are specialized objects).

This: candidate.constructor === Dog

is checking if the value in candidate.constructor is a perfect match for the value in Dog. Since we know that Dog is a function, it is a reference type so really, it is a “reference” or an address in memory. So really it is checking if each points to the same place in memory, to the same function. Since a reference can only point to one thing, if they have the same “thing” in there.

Consider:

let Dog = function Dog(name) {
  this.name = name;
}

let myDog = new Dog('fluffy');
console.clear()
console.log(myDog.constructor === Dog);
// true, the same reference to the same function

Dog = 123;
myDog = { constructor: 123 };
console.log(myDog.constructor === Dog);
// true, the values are the same

Dog = undefined;
myDog = { constructor: undefined };
console.log(myDog.constructor === Dog);
// true, the values are both undefined

const myFunc = () => console.log('howdy!');
Dog = myFunc;
myDog = { constructor: myFunc };
console.log(myDog.constructor === Dog);
// true, because they have the same reference

Dog = () => console.log('howdy!');
myDog = { constructor: () => console.log('howdy!') };
console.log(myDog.constructor === Dog);
// false, because they are two version of the exact same function but with
// different references - the reference matters, not the value stored there
// By default, primitives are compared by value and "reference types" are
// compared by reference.

So, if my “link” you mean what JS means when it says “reference” (other computer languages might have other words like “address” or “pointer”) and replace “comparing it to an object” with “comparing it to a function”, then you are basically right.

[Again, in JS functions are technically objects - everything that isn’t a primitive is some kind of object in JS - but we would normally refer to that as a function.]

Hi, thanks a lot for the detailed reply, the following is the answer i was looking for.

Since we know that Dog is a function, it is a reference type so really, it is a “reference” or an address in memory. So really it is checking if each points to the same place in memory, to the same function.

due to this being my first ever post on the forum, I think i was a bit too generic in explaining the problem i was facing.

yes, by “a link” i meant the reference to that object in memory. which was a bit confusing for me because at first i thought it was comparing the contents of that object instead.

and by the Dog object being compared to the .constructor property, i meant the Dog function in this context being used as a object instead which i assumed is what you would call them when they are used like this.

Dog() = function

Dog = object

or am i missing something here?

let Dog = function Dog(name) {
  this.name = name;
}

console.log(typeof Dog());
// undefined

console.log(typeof new Dog());
// object

console.log(typeof Dog);
// function

Dog is a function that (with the new keyword) creates a new instance of that object constructor. Yes, the instance is an object.

This is confusing stuff. The JS object constructor pattern was something (I assume) was an attempt to implement something like Object Oriented Programming (OOP). Later the class is implemented in JS which is just really a wrapper for object constructors, but again, trying to get a version of OOP. I think it’s not a true version of OOP, but is OOP-ish. But it can be useful.

1 Like

i get it ig. after a while this stuff gets kinda intuitive so you don’t think about the weird stuff that you have to write but anyway, looks like i need to spend time on the javascript OOP concepts. thanks for the help

If you want a deeper understanding of the mechanics of these things, the books You Don’t Know JS by Simpson are amazing. They start out pretty easy, but they eventually get quite advanced. Electronic versions of these are available online for free.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.