角色扮演银行 使用方法更改属性



所以我试图为我的游戏建立一个银行,银行正在汇总价值,但没有改变属性。这是为什么呢?这与范围有关吗?谢谢。。。

我使用了你的代码,IM在Codepen上,当我输入console.log(player.credits(时,即使我输入一个新数字,它也保持为5000积分。

是的,我真的很喜欢你给我编写的代码,它可以工作,但是当我在代码笔中将其登录到控制台时,它仍然没有改变信用......

<p id='move'></p>
<input type='number' id='money' min="1">
<button id='deposit' onclick='player.bank()'>Deposit</button>
let player = {
credits: 5000,
bankCredits: 100,
bank() {
let hope = this.credits || 0;
let a = parseInt(document.getElementById('money').value) || 0;
this.credits = hope + a;
document.getElementById('move').innerHTML = 'You have deposited ' + a + ' and now have ' + this.credits;
}
};
console.log(player.credits) ```




要访问对象函数中的当前对象属性,只需将player.credits替换为this.credits

此外,由于您主要使用数字,因此您可以使用<input type="number"而不是<input type='text'

let player = {
credits: 5000,
bankCredits: 100,
bank() {
let hope = this.credits || 0;
let a = parseInt(document.getElementById('money').value) || 0;
this.credits = hope + a;
document.getElementById('move').innerHTML = 'You have deposited ' + a + ' and now have ' + this.credits;
}
}
console.log(player.credits);
<p id='move'></p>
<input type='number' id='money' min="1">
<button id='deposit' onclick='player.bank()'>Deposit</button>

相关内容

最新更新