Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Console Logging a Method That's Inside an Object

  • This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #198253
    porgeet
    Participant

    Hi 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 private returnBalance 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 method askTeller.

    I’m not sure why I can’t just console.log(john.askTeller());

    I’m also not sure why john.askTeller(); assigned the variable myBalanceMethod then that variable is assigned to the variable myBalance to then be console.log();ed.

    Any explanation or pointing in the right direction would be greatly appreciated.

    Thanks
    porgeet

    #198273
    Chromawoods
    Participant

    The return value you get when calling john.askTeller() is a function itself, which is why you first store it in myBalanceMethod. You could call it directly by doing john.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.

    #198283
    porgeet
    Participant

    Thanks 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 called myBalanceMethod();, then setting a variable equal to that function var 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
    porgeet

    #198451
    porgeet
    Participant

    Hey __

    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 the askTeller function for the object john. That result is the function returnBalance.

    Then I have to call the returned function stored in the variable myBalanceMethod (ie. the returnBalance function. The function returnBalance returns the variable bankBalance stored within the object.) and I call it by adding the (); to the end of it. That is then assigned to the variable myBalance

    So console logging myBalance will log the result of the returnBalance funciton, the bankBalance variable of 7500.

    I see it as a sequence of sorts:

    1. Calling askTeller (whose result is another function) and store it in the variable myBalanceMethod
    2. Calling myBalanceMethod (because it’s equal to a function) and store the returned bankBalance value in myBalance
    3. 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
    porgeet

    #198481
    porgeet
    Participant

    Sweet, thanks a lot for your help :D

    One last thing :P

    Do I add the (); because it’s equal to a function myBalanceMethod. You don’t usually add the brackets to a variable unless they are equal to a function?

    Thanks again :D
    porgeet

Viewing 5 posts - 1 through 5 (of 5 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.