简体   繁体   中英

Javascript - simple OOP encapsulation

let client = {
    main_account_balance: 0,
    balance_setter: function(value) {
      console.log(value);
      console.log('balance_setter');
      this.main_account_balance = value;
    }
};

whenever client's field main_account_balance is being set like this:

client.main_account_balance = 15;

It works perfectly fine. The problem arises whenever I am trying to use:

client.balance_setter(15);

It does not change value of

main_account_balance

at all

Seems to work fine for me.

 let client = { main_account_balance: 0, balance_setter: function(value) { this.main_account_balance = value; //console.log('balance_setter'); //console.log(this.main_account_balance); } }; function increment() { client.balance_setter(client.main_account_balance+1); document.getElementById('value').innerHTML = 'main_account_balance = '+client.main_account_balance } document.getElementById('button').addEventListener('click', increment); 
 <button id="button"> click </button> <h1 id="value"> </h1> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM