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),
这个 bool 值总是返回 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
当前正在使用的对象时,需求部分总是返回“false”,即使我显然有足够的资源。
可能有帮助的背景信息:
- 您从每种货币的 0 开始。
- 可见性函数将每 5 秒触发一次。
- 如果该功能按预期工作,一个按钮将附加到一个空表上,该表具有一个事件监听器,该事件监听器会在单击时升级该项目(具有另一个功能)。
编辑:这是我的资源
对象:
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
谢谢!
最佳答案
requirement
属性在对象的声明中立即设置为 true
或 false
- 在第一次计算后,它不会t 自发地改变自己。您想要一个可以调用的函数,它返回一个 bool 值,而不是一个静态 bool 值:
requirement: () => resources.science.amount >= 40,
然后调用它
if (iter.requirement() && // ...
关于javascript - 我对象中的 bool 值总是返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49622411/