Accessing Private Variables (storing methods in variables)


#1

Why bother storing getBalance() in "var myBalance"?

Why not skip that step and just access John's balance with john.getBalance()?


function Person(first,last,age) {
   this.firstname = first;
   this.lastname = last;
   this.age = age;
   var bankBalance = 7500;
  
   this.getBalance = function() {
      // your code should return the bankBalance
      return bankBalance;
   };
}

var john = new Person('John','Smith',30);
console.log(john.bankBalance);

// create a new variable myBalance that calls getBalance()
var myBalance = john.getBalance();
console.log(john.getBalance());


#2

Storing intermediary results in variables with good names makes it much easier to reason about code, making it possible to understand what that value represents by reading the name instead of reading the full expression.
But yeah, it's fine to skip it here. You did neither though, you requested the value twice.


#3

Okay that's understandable.

We're storing a method in a new variable that' descriptive of its purpose/function so that we don't have to look back up to the actual method and determine what it does.

Can't we just use descriptive language for the original variable that contains the method? In this case ".getBalance" seems to be descriptive enough.


#4

Is the value you are storing really a method?
I might store it in a variable if I thought of it as representing something else, or if I need to use it several times and wouldn't want to request it multiple times, that won't make a significant difference a lot of times though and can just be considered an unnecessary optimization.

Storing the method would look like this:

var myBalanceMethod = john.getBalance;
console.log(myBalanceMethod());

Which you can certainly do, the method will continue to be associated with the object (that's what makes it a method as opposed to a function). But that's not what you were doing.

NEVERMIND I'm lying to you. Methods don't necessarily stay associated with their object, depends on where they were defined.. javascript isn't a full-blown object oriented language.


#5

So in my code the function is ".getBalance".

If myBalance = john.getBalance()...
is "myBalance" the method or...
because john.getBalance is stored in myBalance: THAT is the method...the storing of john.getBalance() in var myBalance?


#6

getBalance refers to a method
myBalance is a variable
calling getBalance returns some value, presumably a number

And the way you've set up your getBalance, you create a new one for each Person, so each and every one has its own getBalance. Another way would be to put it in Person's prototype so that the same function would be shared between all Person's

When you create getBalance, you are creating a closure, the constructor will have exited by the time getBalance is called, but getBalance is keeping that variable in existence despite that the scope has exited. This is only happening because it refers to a variable in a scope that has exited.


#7

I found this...

A function is a piece of code that is called by name. It can be passed data to operate on (i.e. the parameters) and can optionally return data (the return value).

All data that is passed to a function is explicitly passed.

A method is a piece of code that is called by a name that is associated with an object. In most respects it is identical to a function except for two key differences:

A method is implicitly passed the object on which it was called.
A method is able to operate on data that is contained within the class (remembering that an object is an instance of a class - the class is the definition, the object is an instance of that data).
(this is a simplified explanation, ignoring issues of scope etc.)

Will you give me an analogy to help me put it into context? Something not related to programming. Like fruits or sports or movies.

Thanks for all your help, by the way.


#8

That's a good description, don't have an analogy for you.
Javascript is by no means the best language to be learning this either because it's really just a very simple language where people bolt on concepts from other languages and very often get things very wrong, and javascript will let you be wrong and "work" because.. it does as told even if it's told strange things.

You might have an object that represents time, and it might have a method that allows you to add another object that represents a timespan (like 5 minutes)

A function could do the same thing by accepting a time object and a timespan object and modifying the time object, so in a way it's an organisation scheme.

In more advanced code, you may find find functions and methods being treated like any other value. If such a function is bound to a particular object then that object might be affected when its method is called even though the caller has no direct reference to that object (they could probably dig it out of the method though)


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.