Javascript - 简单的 OOP 封装
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;
}
};
每当客户的字段 main_account_balance 设置如下时:
client.main_account_balance = 15;
它工作得很好。每当我尝试使用时都会出现问题:
client.balance_setter(15);
它不会改变
的值
main_account_balance
完全
似乎对我来说很好。
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>
let client = {
main_account_balance: 0,
balance_setter: function(value) {
console.log(value);
console.log('balance_setter');
this.main_account_balance = value;
}
};
每当客户的字段 main_account_balance 设置如下时:
client.main_account_balance = 15;
它工作得很好。每当我尝试使用时都会出现问题:
client.balance_setter(15);
它不会改变
的值main_account_balance
完全
似乎对我来说很好。
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>