[英]Could someone explain how arrays inside of objects work? What is wrong with this code?
I am a beginner and i decided to make a game to practice, and i immediately ran into problems. 我是一个初学者,所以我决定制作一个游戏来练习,但我立即遇到了问题。 This is my code.
这是我的代码。 I think it explains itself.
我认为这说明了自己。
<!DOCTYPE>
<html>
<body>
<script>
var user = {
name: this.name,
species: this.species,
stats: [this.strength=0, this.wit=0, this.magic=0, this.health=0]
}
user.name = prompt("What is your name?");
user.species = prompt("Hello, " + user.name + ". What is your species? Orc, Human, Elf, Troll, or Mage.");
user.species = user.species.toLowerCase;
//Sets the stats according to what the user said
switch(user.species){
case "orc":
user.stats=[user.strength=4, user.wit=2, user.magic=2, user.health=45];
break;
case "human":
user.stats=[user.strength=3, user.wit=3, user.magic=3, user.health=40];
break;
case "troll":
user.stats=[user.strength=5, user.wit=1, user.magic=1, user.health=50];
break;
case "elf":
user.stats=[user.strength=3, user.wit=4, user.magic=3, user.health=35];
break;
case "mage":
user.stats=[user.strength=3, user.wit=4, user.magic=3, user.health=30];
break;
}
//It is supposed to output the strength
document.write(user.stats[0]);
</script>
</body>
</html>
The problem is no matter what I enter it always prints "0". 问题是无论我输入什么,它总是打印“ 0”。 Could someone please explain the problem?
有人可以解释这个问题吗?
user.species.toLowerCase
needs to be user.species.toLowerCase()
. user.species.toLowerCase
必须是user.species.toLowerCase()
。 By not calling the function you assign the function to user.species and thus it never matches any of the cases. 通过不调用该函数,可以将该函数分配给user.species,因此它从不匹配任何一种情况。 You should also add a
default
case to catch these issues. 您还应该添加
default
案例来捕获这些问题。
Replace your code with - 将您的代码替换为-
user.species = user.species.toLowerCase();
toLowerCase is a function. toLowerCase是一个函数。
Reference - http://www.w3schools.com/jsref/jsref_tolowercase.asp 参考-http: //www.w3schools.com/jsref/jsref_tolowercase.asp
您在user.species = user.species.toLowerCase上缺少();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.