Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Object-Oriented JavaScript (2015) Introduction to Methods Adding a Method to an Object

Why can't you just call the function diceRoll()? Is it always necessary to create a object then a method?

,

1 Answer

Josh Olson
Josh Olson
13,236 Points

I'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.

Thank 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!!!