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 trialmfod
1,708 PointsWhy can't you just call the function diceRoll()? Is it always necessary to create a object then a method?
,
1 Answer
jobbol
Full Stack JavaScript Techdegree Student 17,885 PointsI'm not 100% sure what you're asking. Are you saying objects are over-complicating a simple task? Or you need help understanding objects and their methods? Elaborate on this please.
1. A syntax question
If you're asking about the syntax itself. "How do I use this?"
Because he moved the function onto the dice object, you must type the object name, dot, and property to access the property. If you did try to call it as diceRoll()
itself, it will look in the global scope for it.
//definition
var a = {b:2};
//call
console.log( a.b );
//definition
var dice = {roll:function(){return 2}};
//call
console.log( dice.roll() );
Or 2. A "this is overly-complicated" question
Why can't you just call the function diceRoll()?
If you're saying he's making this more complex than it needs to be. In something as simple as rolling a dice, yes this could have been done easier. You could have created diceRoll at the global scope like this - with no object, no method, only the function itself.
//definition
function diceRoll(){return 2;}
//call
console.log( diceRoll() );
However this defeats the purpose of the lesson: making use of objects. Objects help you distinguish parts of your code and they establish ownership. The roll function belongs to the dice object. Suppose you did continue creating everything at the global scope for a board game. (Don't do this).
You added a few players...
var player1;
var player2;
var player3;
var player4;
Then added a way to keep track of money...
var money1;
var money2;
var money3;
var money4;
And a way to keep track of cards...
var cards1 = [];
var cards2= [];
var cards3= [];
var cards4 = [];
Now it's hard to tell what belongs to what. As you can see this becomes a mess very quickly, and this is where objects come in. The simple diceRoll example could have easily been done without objects, but Andrew chose this example to prep you for the more challenging things to come. Because once you start getting more in depth, then it starts turning into this mess.
mfod
1,708 Pointsmfod
1,708 PointsThank you for explaining. My question was more toward your 2nd point. I understood how it works. I was just a little confused as to why it was over complicated. Now I understand better the purpose of objects. Thanks so much!!!