【问题标题】:Unexpected token 'this' (javascript)意外的令牌“this”(javascript)
【发布时间】:2021-08-03 12:46:56
【问题描述】:

我是编码新手,这是我的第一篇文章。非常感谢您提供的任何帮助。我正在尝试创建一个级别计算系统,您可能会在 RPG 中看到具有不同统计数据的角色。当我以不同的值运行程序时,我收到以下消息:

意外的标记“this”

我已将出现错误消息的代码部分放在星号中。

const Sean = {
    fullName: `Sean`,
    attack: 1,
    strength: 1,
    defense: 38,
    hitpoints: 40,
    prayer: 15,
    range: 98,
    magic: 99,
    calcCombatLevel: function () {
        this.Meleelvl = .25 * (this.defense + this.hitpoints + Math.floor((this.prayer / 2))) + ((13 / 40) * (this.attack + this.strength));

        this.magiclvl = .25 * (this.defense + this.hitpoints + Math.floor((this.prayer / 2))) + ((13 / 40) * (this.magic * (3 / 2)));

        this.rangelvl = 0.25 * (this.defense + this.hitpoints + Math.floor((this.prayer / 2))) + ((13 / 40) * (this.range * (3 / 2)));


        if ((this.attack + this.strength) > ((2 / 3) * this.magic) && (this.attack + this.strength) > ((2 / 3) * this.range)) {
            return this.meleelvl;
        }
       // **********************************
       else if ((2 / 3) this.magic > (this.attack + this.strength) && (this.magic >** this.range)) {
            return this.magiclvl;
        }
        else if((2 / 3) * this.range > (this.attack + this.strength) && (this.range > this.magic)) {
        return this.rangelvl;
            }
        }
    }
Sean.calcCombatLevel();
console.log(Math.floor(Sean.lvl));

【问题讨论】:

  • 在 (2 / 3) 之后需要一个星号来表示乘法。
  • 这是哪种编程语言?请edit澄清,并添加标签。
  • 感谢您的帮助!!!我输入了星号,但现在我输入的任何值在我运行程序时都会得到消息而不是数字。我正在使用 Javascript
  • 请将其作为一个单独的问题发布,并包含完整的回溯。现在,我看不到你在哪里初始化 Sean.lvl
  • 感谢所有发帖的人。我意识到我没有问最好的问题,以后我会更好地遵循论坛指南。艾莉亚·麦卡锡,你是对的,我在 (2/3) 之后错过了星号

标签: javascript unexpected-token


【解决方案1】:

第一个 elseif 有:

(this.magic >** this.range)
// also missing * at start, has (2/3) magic, where the 'this' error is from

我认为你的意思是:

else if ((2 / 3) * this.magic > (this.attack + this.strength) && (this.magic > this.range)) {

没有玩过代码,但这是唯一让我印象深刻的第一个明显的事情。

正如上面有人评论的那样,Sean.lvl 似乎没有被定义,所以最后的输出可能也不起作用。您可以改为执行以下操作(或定义一个名为 lvl 的字段):

var mylvl = Math.floor(Sean.calcCombatLevel());
console.log(mylvl);

至少在一个地方你的变量名有不一致的大小写,this.Meleelvl 和 this.meleelvl,也解决这个问题(函数的第一行应该有 this.meleelvl,保持所有变量名小写)。

【讨论】:

  • 嗨史蒂夫。谢谢你的帮助。问题在于我用于变量名的大小写。当我固定外壳时,程序运行正常。我还必须按照你和其他人所说的来定义变量 Sean.lvl
猜你喜欢
  • 2014-11-29
  • 2015-06-11
  • 1970-01-01
  • 1970-01-01
  • 2021-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多