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;即使我有足够的资源。
可能有用的背景信息:
编辑:这是我的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
谢谢!
答案 0 :(得分:3)
requirement
属性会立即在对象的声明中设置为true
或false
- 在第一次评估之后,它不会自发地自行更改。你想要一个可以调用返回布尔值而不是静态布尔值的函数:
requirement: () => resources.science.amount >= 40,
然后用
调用它if (iter.requirement() && // ...