JavaScript 变量在赋值后显示为未定义

标签 javascript variables console undefined

// Initialise Variables
	// type: 1 = primary	2 = secondary	3 = refined
var food = {
	name:'food',
	type:1,
	total:0,
	increment:1,
	secondary:'skins',
	secondaryChance:0.1
},
wood = {
	name:'wood',
	type:1,
	total:0,
	increment:1,
	secondary:'herbs',
	secondaryChance:0.1
},
stone = {
	name:'stone',
	type:1,
	total:0,
	increment:1,
	secondary:'ore',
	secondaryChance:0.1
},
skins =  {
	name:'skins',
	type:2,
	total:0,
	refined:'leather',
	increment:1
},
herbs =  {
	name:'herbs',
	type:2,
	total:0,
	refined:'belief',
	increment:1
},
ore =  {
	name:'ore',
	type:2,
	total:0,
	refined:'metal',
	increment:1
},
leather = {
	name:'leather',
	type:3,
	total:0,
	increment:0.5
},
belief = {
	name:'belief',
	type:3,
	total:0,
	increment:0.5
},
metal = {
	name:'metal',
	type:3,
	total:0,
	increment:0.5
}

// Clicker type (who created the resource)
player = 0
gatherer = 0

// Called when primary resource is clicked
	
function primaryResourceClick(resource){
	resource.total += resource.increment;
	console.log(resource + resource.total);
	x = Math.random();
	console.log(x);
	if (resource.name == 'food'){
		document.getElementById("food").innerHTML = food.total;
		if (x < food.secondaryChance) {
			skins += skins.increment;
			document.getElementById("skins").innerHTML = skins.total;
			console.log(skins + skins.total);
		}
	}
	if (resource.name == 'wood') {
		document.getElementById("wood").innerHTML = wood.total;
		if (x < wood.secondaryChance) {
			herbs += herbs.increment;
			document.getElementById("herbs").innerHTML = herbs.total;
			console.log(herbs + herbs.total);
		}
	}
	if (resource.name == 'stone') {
		document.getElementById("stone").innerHTML = stone.total;
		if (x < stone.secondaryChance) {
			ore += ore.increment;
			document.getElementById("ore").innerHTML = ore.total;
			console.log(ore + ore.total)
		}
	}
};
<html>
<head>
		<title>Village</title>
	
		<link rel="stylesheet" type="text/css" href="interface.css" />

		<script language="javascript" src="main.js"></script>
</head>

<body>

<div id="resources">
	<div id="primaryResources">
		<h4>Primary Resources</h4>
		<table id="primaryResourcesDisplay">
			<tr>
				<td><button type="button" onclick="primaryResourceClick(food)">Gather Food</button></td>
				<td>Food:</td>
				<td><span id="food">0</span></td>
			</tr>
			<tr>
				<td><button type="button" onclick="primaryResourceClick(wood)">Cut Wood</button>
				<td>Wood:</td>
				<td><span id="wood">0</span></td>
			</tr>
			<tr>
				<td><button type="button" onclick="primaryResourceClick(stone)">Mine Stone</button>
				<td>Stone:</td>
				<td><span id="stone">0</span></td>
			</tr>
		</table>
	</div>
	<div id="secondaryResources">
		<h4>Secondary Resources</h4>
		<table id="secondaryResourcesDisplay">
			<tr>
				<td>Skins:</td>
				<td><span id="skins">0</span></td>
				
				<td>Leather:</td>
				<td><span id="leather">0</span></td>
			</tr>
			<tr>
				<td>Herbs:</td>
				<td><span id="herbs">0</span></td>
				
				<td>Belief:</td>
				<td><span id="belief">0</span></td>
			</tr>
			<tr>
				<td>Ore:</td>
				<td><span id="ore">0</span></td>
				
				<td>Metal:</td>
				<td><span id="metal">0</span></td>
			</tr>
		</table>
	</div>
</div>

</body>
<html>

每当更新任何辅助资源时,它都会显示为未定义。我尝试将一些数据记录到控制台,控制台也将变量显示为未定义。更奇怪的是控制台没有显示任何错误。

最佳答案

问题是试图通过skins.increment增加对象皮肤,而不是通过skins.increment增加skins.total。比其他任何事情都更重要的是打字错误。已在评论中解决。

关于JavaScript 变量在赋值后显示为未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26463939/

相关文章:

javascript - 在 AngularJS 中切换自定义排序图标

jquery - 使用链接的一部分作为变量

Bash 随机化变量

C++ winsock 在 bind() 上给出 10038 错误

javascript - jQuery - 在代码行之前查找特定元素

javascript - 如何将 block 按到主窗口底部?

javascript - Angular 指令和 Controller 响应 ng-click 事件

javascript - 将变量传递给 Angular 过滤器

java - 通过java在mac中打开终端

javascript - 如何在页面加载时执行控制台 Javascript?