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 Methods with Prototypes

So why is dice.roll and dice10.roll false?

I still don't see how how both function references are false. Any help in understanding this will go a long way.

 function sayHi(str){
    alert(str);
}

function sayHi(str){
    alert(str);
}

console.log(sayHi === sayHi);

 //true

In this example, the output is true.

Thank you!

3 Answers

Steven Parker
Steven Parker
229,786 Points

You may not realize it, but you only have one function there. The 2nd definition of sayHi replaces the first one. So when you do the compare, you are comparing the one function with itself, so the result is true.

Now if you change the name of one of your functions, for example to "sayHi2", and then do this comparison:

console.log(sayHi === sayHi2);

The result will be false, since they are different functions (that happen to do the same thing).

You did not include any code for the "dice.roll" and "dice10.roll" functions you mention, but I would assume that they compare false because they are also different functions.

rakan omar
rakan omar
15,066 Points

look at the code inside the dice roll function, you will see that their number of sides is referenced. so in dice.roll the function basically says "get a random number from 1 to 6" while the dice10.roll says "get a random number from 1 to 10".

Jeremy Castanza
Jeremy Castanza
12,081 Points

Think of the constructor function as a printer. You decide to print a news article. You go ahead and print the same news article again five minutes later.

Both pieces of paper are instances (the output from the constructor function). While each instance looks alike, they're still separate pieces of paper. You could write on one and not the other. Now, think of each piece of paper as a container for your code. You could have different notes on one and not the other - or in this case, each instance could have its own method.

The same principle applies with the dice.roll === dice10.roll being equal to false.

hi jeremy, can you also tell me why this code still runs? i thought it doesn't unless every line is true.