Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8088599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:08:23+00:00 2026-06-05T19:08:23+00:00

I didn’t take any official course of Javascript. I often read this in tutorials:

  • 0

I didn’t take any official course of Javascript.

I often read this in tutorials:

function Bank(name) {
   this.name = name;
   var balance = 100;

   // *my question here* method to access balance
   this.getBalance = function() {
       return balance * 0.9 ;
   };
}

var myBank = new Bank("My Bank");
console.log( myBank.getBalance() );

Why do developers use getBalance() instead of getBalance?

I think “getBalance” is easier to read, but no tutorial use this:

// *my question here* method to access balance
   this.getBalance = balance * 0.9 ;

then

console.log( myBank.getBalance );

I didn’t see any tutorial to write using above style, even it is simpler. So:

Is there any different between 2 style of codes? Is there any concern to avoid .getBalance to operate the private variable?

Thanks in advance to explain.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T19:08:25+00:00Added an answer on June 5, 2026 at 7:08 pm

    You must understand the different between variable, function and function call. This is variable:

    this.getBalance = balance * 0.9 ;
    

    The value of getBalance variable is computed once when the code is executed. Every time you access getBalance (without parentheses), previously computed value is returned.

    There is an important implication of using getBalance variable. If you modify underlying balance variable in the future, getBalance won’t reflect that change because it was computed using old balance value.

    In the case below:

    this.getBalance = function() {
       return balance * 0.9 ;
    };
    

    getBalance is also variable but it points to a function, not to a simple value. If you access it like variable, you will get a function reference:

    var fun = this.getBalance
    

    You can now call that function using fun(), but fun reference itself (same as this.getBalance) isn’t very useful in your case.

    And of course you can call the function by using this.getBalance(). The semantic difference is as follows: every time you call getBalance(), the body of the function is executed. This means the value of balance is read again and getBalance() return value is always up to date.

    As you can see there is a huge difference between assigning a function and assigning a simple value (like number) to a variable. Depending on which approach you choose, it will have different implications. You must learn about functional side of JavaScript in order to fully utilize its power.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I didn't understand this. This is used for filtering case insensitive: jQuery.expr[':'].Contains = function(a,i,m){
I didn't find any info on searches when I looked this up. I've been
I didn't find much in tutorials on this specific question.. So I have a
Didn't know how else to put this. Say I have a JavaScript method that
I didn't see any similar questions asked on this topic, and I had to
I didn't see any questions that asked this previously so here goes: Once you
Didn't got any luck finding an answer on google , so this is my
Didn't find any from their website. I mostly just trying to see which one
I didn't find an answer on the net and i hope this question have
i didn't tested this code on iPhone but i'm sure (tested) it doesn't works

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.