我可以使用getter在js中追溯应用值吗?

时间:2018-04-08 20:30:16

标签: javascript

我有一个对象player,其中包含以下内容:

var player = {
    moneyRatio: 3, //Multiplier of money as a function of number.
    globalMultiplier: 1.000 //Total multiplier, multiplies ALL production.
};

我有一个名为benefits的特殊升级版本,它应该会使player.moneyRatio每个版块gradStudent减少0.002。

{
    name: "Benefits",
    desc: "Your graduate students seem more professional, giving them a bonus to money scaling.",
    moneyCost: 200,
    scienceCost: null,
    popularityCost: null,
    requirement: () => RESOURCES[0].count >= 10,
    result : function() {
        player.moneyRatio = /* ??? */
    },
    id: 'benefitsUpg', //Id used to fetch stuff
    html: `<button id="benefitsUpgButton" class="upgradeButtons">Benefits</button>     
    <div class='tooltip' id='benefitsUpgTooltip'> 
        <span>Your graduate students seem more professional, giving them a bonus to money scaling.<br /> </span>
        <span class='tooltipMoneyColor'> <span class='boldedNumbers' id='benefitsUpgMoneyCostDisp'> </span><br /> dollars </span>
        <span>----------------------------------------------------------------</span>
            <div>Money Scaling Ratio: -0.002 per grad student </div>

    </div>`,
    index: 1 //Index to determine what is deleted when you buy the upgrade. Based ONLY upon what order the table is sorted by.
 },

我想追溯性地应用它,所以如果玩家用5名研究生购买benefits,游戏将自动从moneyRatio减去0.01。我想要确保每次购买新的gradStudent时都会应用-0.002。

有没有办法只在每次升级中执行此操作?我希望result函数更改moneyRatio以便它会自动更新。我不想为这样的每个升级创建特殊函数和if / else语句(我计划有超过400个这样的因此,为每个特殊情况添加if / else块会变得非常麻烦。

我尝试使用get“函数”(?),但我真的不知道我是否可以将它用于此目的,如果可以,我真的不知道该怎么做。

谢谢!

0 个答案:

没有答案