Home › Forums › JavaScript › Console Logging a Method That's Inside an Object
- This topic is empty.
-
AuthorPosts
-
March 15, 2015 at 4:57 pm #198253
porgeet
ParticipantHi guys and girls
I’m doing some courses on Codecademy to teach myself Javascript, every now and then I’ll complete a course but I’m not entirely sure what the code does, or why it’s done that way.
This particular course is teaching me about variables being private or public within an object. And how to access something that has been made private using the
var
keyword. In this case it’s the privatereturnBalance
function.I was hoping you good people could shed some light on it for me please?
Here is the code:
function Person(first,last,age) { this.firstname = first; this.lastname = last; this.age = age; var bankBalance = 7500; var returnBalance = function() { return bankBalance; }; // create the new function here this.askTeller = function() { return returnBalance; }; } var john = new Person('John','Smith',30); console.log(john.returnBalance); var myBalanceMethod = john.askTeller(); var myBalance = myBalanceMethod(); console.log(myBalance);
I’m puzzled by the bottom part where I
console.log
the methodaskTeller
.I’m not sure why I can’t just
console.log(john.askTeller());
I’m also not sure why
john.askTeller();
assigned the variablemyBalanceMethod
then that variable is assigned to the variablemyBalance
to then beconsole.log();
ed.Any explanation or pointing in the right direction would be greatly appreciated.
Thanks
porgeetMarch 16, 2015 at 5:52 am #198273Chromawoods
ParticipantThe return value you get when calling
john.askTeller()
is a function itself, which is why you first store it inmyBalanceMethod
. You could call it directly by doingjohn.askTeller()()
but that’s a bit weird.I’m not sure what the restrictions of this particular course are, but instead of askTeller returning
return returnBalance;
you could instead return the result of it:return returnBalance();
. But again, I don’t know if that would violate what the course is trying to teach you.March 16, 2015 at 7:12 am #198283porgeet
ParticipantThanks for your help.
You pretty much just do what you’re told in it, it can be quite limited in the amount of wiggle room you have to experiment.
It just teaches you that object keys can be accessed from outside the object. As soon as you use the
var
keyword their local scope makes them effectively private.You can do the same with functions defined within the object constructor.
Then it teaches you that you can make something access an object’s keys using a function without the
var
keyword.The bottom code was populated by the course itself, but the explanation of why it’s made like that is not very clear.
The way I understand it there is a difference between calling a
function();
where the function would be activated expecting arguments etc.? And calling the function’s outputted value where you would exclude the()
.Is that why the bottom part of the code is the way it is?
If so why not just
console.log(myBalanceMethod);
?Does setting
var myBalanceMethod = john.askTeller();
create an empty function calledmyBalanceMethod();
, then setting a variable equal to that functionvar myBalance = myBalanceMethod();
creates a variable equal to all of that, so that it can then be console logged without calling it?It looks to me like it’s just a way to get around having to call the function and just console log it’s result which is already going to be set.
That’s my understanding so far :P
Sorry about the verbose post
Thanks again for your help
porgeetMarch 18, 2015 at 7:38 am #198451porgeet
ParticipantHey __
Thanks a lot for that explanation, that really helped :)
Just to be sure I’m understanding it right:
var myBalanceMethod = john.askTeller();
assigns the result of theaskTeller
function for the objectjohn
. That result is the functionreturnBalance
.Then I have to call the returned function stored in the variable
myBalanceMethod
(ie. thereturnBalance
function. The functionreturnBalance
returns the variablebankBalance
stored within the object.) and I call it by adding the();
to the end of it. That is then assigned to the variablemyBalance
So console logging
myBalance
will log the result of thereturnBalance
funciton, thebankBalance
variable of 7500.I see it as a sequence of sorts:
- Calling askTeller (whose result is another function) and store it in the variable myBalanceMethod
- Calling myBalanceMethod (because it’s equal to a function) and store the returned bankBalance value in myBalance
- myBalance contains the end result of that sequence the bankBalance variable which is the 7500 number
I think I may have just written the same thing twice there :P
If I had more functions accessing other functions that eventually accessed the variable I needed, that process of assigning
called
functions to variables would just go on and on until I reached the end result of the variable, is that about right?Cheers
porgeetMarch 18, 2015 at 4:06 pm #198481porgeet
ParticipantSweet, thanks a lot for your help :D
One last thing :P
Do I add the
();
because it’s equal to a functionmyBalanceMethod
. You don’t usually add the brackets to a variable unless they are equal to a function?Thanks again :D
porgeet -
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.