hey all, just finished this question but i don’t really know where num came from at the end, and i also don’t see the reason for putting it. could someone explain why we use this? thanks
I am confused. You wrote the code referenced above, but do not understand why you wrote/used the variable num in the solution?
It’s a parameter for the
setLoot method of the
Cake object. The name
num isn’t particularly important, you can think of it as a placeholder for the real data that’s passed in.
var myCake = new Cake; //create a new Cake object myCake.getLoot(); // => 2 (the initial value) myCake.setLoot(5); myCake.getLoot(); // => 5
Yeah I got that code from one of the examples and couldn’t figure out why they put it in there.
Ok so then I am calling whatever goes between the parentheses? It doesn’t matter what I call it num could have been some other word but it just represents what later is put in between the parentheses when I run the code…? Is that right?
Correct. Here’s a simpler example:
Great thank you for the help! i think im starting to understand the basics!
Set / Get are common methods written for objects to maintain local scope to the object only.
C++ really drove home only allowing access to objects through methods, keeping all the variables / parameters sealed up locally for me, long ago.
This is the same methodology. Instead of calling a property directly for an object and changing its value you call a function (method) to access the inner workings of the object.
It is all about scope and security.
Constructor, Deconstruct-or, Set, Get those are all popular Object methods in OOP.