JavaScript переменная отображается как неопределенные, когда оно было присвоено значение
// Initialise Variables
\t // type: 1 = primary \t 2 = secondary \t 3 = refined
var food = {
\t name:'food',
\t type:1,
\t total:0,
\t increment:1,
\t secondary:'skins',
\t secondaryChance:0.1
},
wood = {
\t name:'wood',
\t type:1,
\t total:0,
\t increment:1,
\t secondary:'herbs',
\t secondaryChance:0.1
},
stone = {
\t name:'stone',
\t type:1,
\t total:0,
\t increment:1,
\t secondary:'ore',
\t secondaryChance:0.1
},
skins = {
\t name:'skins',
\t type:2,
\t total:0,
\t refined:'leather',
\t increment:1
},
herbs = {
\t name:'herbs',
\t type:2,
\t total:0,
\t refined:'belief',
\t increment:1
},
ore = {
\t name:'ore',
\t type:2,
\t total:0,
\t refined:'metal',
\t increment:1
},
leather = {
\t name:'leather',
\t type:3,
\t total:0,
\t increment:0.5
},
belief = {
\t name:'belief',
\t type:3,
\t total:0,
\t increment:0.5
},
metal = {
\t name:'metal',
\t type:3,
\t total:0,
\t increment:0.5
}
// Clicker type (who created the resource)
player = 0
gatherer = 0
// Called when primary resource is clicked
\t
function primaryResourceClick(resource){
\t resource.total += resource.increment;
\t console.log(resource + resource.total);
\t x = Math.random();
\t console.log(x);
\t if (resource.name == 'food'){
\t \t document.getElementById("food").innerHTML = food.total;
\t \t if (x < food.secondaryChance) {
\t \t \t skins += skins.increment;
\t \t \t document.getElementById("skins").innerHTML = skins.total;
\t \t \t console.log(skins + skins.total);
\t \t }
\t }
\t if (resource.name == 'wood') {
\t \t document.getElementById("wood").innerHTML = wood.total;
\t \t if (x < wood.secondaryChance) {
\t \t \t herbs += herbs.increment;
\t \t \t document.getElementById("herbs").innerHTML = herbs.total;
\t \t \t console.log(herbs + herbs.total);
\t \t }
\t }
\t if (resource.name == 'stone') {
\t \t document.getElementById("stone").innerHTML = stone.total;
\t \t if (x < stone.secondaryChance) {
\t \t \t ore += ore.increment;
\t \t \t document.getElementById("ore").innerHTML = ore.total;
\t \t \t console.log(ore + ore.total)
\t \t }
\t }
};
<html>
<head>
\t \t <title>Village</title>
\t
\t \t <link rel="stylesheet" type="text/css" href="interface.css" />
\t \t <script language="javascript" src="main.js"></script>
</head>
<body>
<div id="resources">
\t <div id="primaryResources">
\t \t <h4>Primary Resources</h4>
\t \t <table id="primaryResourcesDisplay">
\t \t \t <tr>
\t \t \t \t <td><button type="button" onclick="primaryResourceClick(food)">Gather Food</button></td>
\t \t \t \t <td>Food:</td>
\t \t \t \t <td><span id="food">0</span></td>
\t \t \t </tr>
\t \t \t <tr>
\t \t \t \t <td><button type="button" onclick="primaryResourceClick(wood)">Cut Wood</button>
\t \t \t \t <td>Wood:</td>
\t \t \t \t <td><span id="wood">0</span></td>
\t \t \t </tr>
\t \t \t <tr>
\t \t \t \t <td><button type="button" onclick="primaryResourceClick(stone)">Mine Stone</button>
\t \t \t \t <td>Stone:</td>
\t \t \t \t <td><span id="stone">0</span></td>
\t \t \t </tr>
\t \t </table>
\t </div>
\t <div id="secondaryResources">
\t \t <h4>Secondary Resources</h4>
\t \t <table id="secondaryResourcesDisplay">
\t \t \t <tr>
\t \t \t \t <td>Skins:</td>
\t \t \t \t <td><span id="skins">0</span></td>
\t \t \t \t
\t \t \t \t <td>Leather:</td>
\t \t \t \t <td><span id="leather">0</span></td>
\t \t \t </tr>
\t \t \t <tr>
\t \t \t \t <td>Herbs:</td>
\t \t \t \t <td><span id="herbs">0</span></td>
\t \t \t \t
\t \t \t \t <td>Belief:</td>
\t \t \t \t <td><span id="belief">0</span></td>
\t \t \t </tr>
\t \t \t <tr>
\t \t \t \t <td>Ore:</td>
\t \t \t \t <td><span id="ore">0</span></td>
\t \t \t \t
\t \t \t \t <td>Metal:</td>
\t \t \t \t <td><span id="metal">0</span></td>
\t \t \t </tr>
\t \t </table>
\t </div>
</div>
</body>
<html>
Всякий раз, когда какой-либо из вторичных ресурсов обновляется, она показывает, как неопределенные. Я попытался зарегистрировать некоторые данные в консоли, консоль также показывает переменную как неопределенную. Еще страннее то, что консоль не показывает никаких ошибок.
Можете ли вы объяснить эту строку? скины + = скины. – graemeboy
'skins.increment' равен' 1'. Следовательно, скины увеличиваются на единицу. –
Вы хотите написать, что skins.total + = skins.increment? – graemeboy