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

Greg Schudel
Greg Schudel
4,090 Points

why is Dice uppercase

I'm lost at why there are upper and lowercase 'd's for the Dice function and instances. How does javascript understand which is which, I thought this was a syntax no no. Why is it allowed here?

2 Answers

Andrew Kiernan
Andrew Kiernan
26,892 Points

Hi Greg!

Upper and lower case letters are all valid for javascript variable and function names, and javascript can distinguish between them. For example,

// All of these are valid variable names, 
// and all would be separate variables (JS can distinguish the names as being different)

var dice; 
var Dice;
var DICE;
var DIce;
var diCE;

Dice is perfectly valid for the constructor name. It is capitalized as a convention to signal to other developers (or to yourself when you go back to it days/weeks/months later) that the function is a constructor function.

Hope that helps!

Greg Schudel
Greg Schudel
4,090 Points

Then why is 'Dice' getting an error of undefined in this case unless it is lower case 'dice'

function dice(sides){

  this.sides = sides;

  this.roll = function() {
    return Math.floor(Math.random() * this.sides) + 1; 

  }

}

// this gets an error, says Dice is undefined, unless it's all lowercase
var dice6 = new Dice(6);

dice6.roll();
Andrew Kiernan
Andrew Kiernan
26,892 Points

Because your function is named 'dice' (all lowercase), and you haven't defined a function called 'Dice'. JavaScript recognizes upper and lower case letters, and they are not interchangeable. So 'dice' != 'Dice'. Whatever name you give your constructor function, that is the name you need to use to create objects.

Greg Schudel
Greg Schudel
4,090 Points

Thank you SOOO much for the guidance!