Can someone explain the use of num in this code

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

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
2 Likes

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:

function fun1(num) {
  return num + 1;
}
fun1(5); // => 6

function fun2(arbitraryName) {
  return arbitraryName + 1;
}
fun2(5); // => 6

function fun3(num) {
  return Num + 1;
}
fun3(5); // ReferenceError: Num is not defined
// This happens because JavaScript is case sensitive
// `Num` does not refer to `num`
2 Likes

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.

@dpbrinkm
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.

-WWC

1 Like