所以我在下面有这个代码(所有的javascript)。我希望获得用户输入游戏的投票数
function Game(gamename,votes) {
this.gamename = gamename;
this.votes = votes;
};
var lol = new Game("League of Legends",1100);
var dota = new Game("DOTA 2",2100);
var ql = new Game("Quakelive",3100);
var csgo = new Game("Counter Strike: GO",4100);
function PostVotes(gnshort){
//string names - working
console.log(gnshort + 'name');
console.log(gnshort + 'votes')
var CalcVotes = function(gnshort){
var votecount = gnshort.votes;
console.log(votecount);
}
CalcVotes(gnshort);
//CalcVotes(lol); //works
};
PostVotes('lol');
调用CalcVotes(gnshort)时,我一直收到错误。而且我知道这不是它传递大声笑的功能,因为它是一个字符串而不是变量或其他东西。我过去一周只学习过javascript,所以任何建议都会有所帮助
答案 0 :(得分:0)
PostVotes( '洛尔');将lol作为字符串传递('lol'相当于“lol”)。你需要做的就是传递变量lol,如
PostVotes(lol);
它将返回lol.votes,又名1100。