我的对象中的布尔值总是返回false

时间:2018-04-03 04:45:12

标签: javascript html

Followup to another question here

目前我有这个对象,它是一个升级对象。

{
    name: "Telling a Friend",
    desc: "It all has to start somewhere... \n Money moneyCost ratio reduction: -0.1% \n Popularity: 0.003/sec",
    moneyCost: 30000,
    scienceCost: null,
    popularityCost: null,
    requirement: (resources.science.amount >= 40),
    buildingAffected: player.moneyRatio,
    buildingCurrencyAffected: null,/* The 'building' is just a variable that controls price ratio reduction. */
    upgVal: 0.999,
    upgProdResource: 'popularity',
    upgProdAmount: (0.003/7),
    id: 'tellingAFriendUpg', //Id used to fetch stuff
    html: `<button id="tellingAFriendUpgButton" class="upgradeButtons">Telling A Friend </button>     
    <div class='tooltip' id='tellingAFriendUpgTooltip'> 
        <span>You have to get the word out somehow! Bore your friend with your (currently) useless number. <br /> </span>
        <span class='tooltipMoneyColor'> <span class='boldedNumbers' id='tellingAFriendUpgMoneyCostDisp'> 30,000 </span> dollars<br /> </span>
        <span>----------------------------------------------------------------</span>
            <div>Money Cost Ratio: -0.1% (multiplicative)</div>
            <div class ='tooltipPopularityColor'>Popularity: 0.003/sec</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.
 },

我关注的是这一行:requirement: (resources.science.amount >= 40),

这个布尔总是返回false,我不知道为什么。我试图通过这个功能:

function checkVisiblityOnUpgrades() {
for (let iter of UPGRADES) {
    var currentUpgrade = iter;
   console.log(iter);
    if (iter.requirement && UPGRADESPURCHASED.includes(iter.id) == false) {
    console.log('hello')
        /* Null Checks before updating html */
        var table = getId('upgradeTable');    
        table.appendChild(tr = document.createElement("tr"));
        tr.appendChild(td = document.createElement("td"));
        td.innerHTML = iter.html;
        console.log(td.innerHTML);
        console.log(td);
// this references the button:
        td.children[0].addEventListener('click', () => {
        upgrade(UPGRADES[returnIndex(UPGRADES,iter.id)]);
        });
        if ((getId(currentUpgrade.id.concat("ScienceCostDisp")) != null))
            getId(currentUpgrade.id.concat("ScienceCostDisp")).innerHTML = currentUpgrade["scienceCost"];
        if (getId(currentUpgrade.id.concat("MoneyCostDisp")) != null)
            getId(currentUpgrade.id.concat("MoneyCostDisp")).innerHTML = currentUpgrade["moneyCost"];
        if (getId(currentUpgrade.id.concat("PopularityCostDisp")) != null)
            getId(currentUpgrade.id.concat("PopularityCostDisp")).innerHTML = currentUpgrade["popularityCost"];

        }
    }

}

但是,当我尝试console.log iter当前正在使用的对象时,需求部分始终返回&#34; false,&#34;即使我有足够的资源。

可能有用的背景信息:

  • 从每种货币的0开始。
  • 可见度功能将每5秒触发一次。
  • 如果函数按预期工作,则会将一个按钮附加到一个空表上,该表具有一个eventListener,用于在单击时升级该项(使用其他函数)。

编辑:这是我的resources对象:

var resources = { //Handles resources of all kinds.
number: 1000000000,
money: {
    amount: 0, // Raw amount, unrounded until numFormat().
    upgProd: 0 //Amount gained from upgrades per TICK.
    },
science: {
    amount: 0,
    upgProd: 0
    },
popularity: {
    amount: 0,
    upgProd: 0 //Amount produced from upgrades.
    }
};

Picture of my currencies at the time

Picture of my object that is printed in the console

谢谢!

1 个答案:

答案 0 :(得分:3)

requirement属性会立即在对象的声明中设置为truefalse - 在第一次评估之后,它不会自发地自行更改。你想要一个可以调用返回布尔值而不是静态布尔值的函数:

requirement: () => resources.science.amount >= 40,

然后用

调用它
if (iter.requirement() && // ...