【问题标题】:Syntax error in javascript: missing ; before statementjavascript 中的语法错误:缺少;声明之前
【发布时间】:2016-01-03 10:35:05
【问题描述】:

我正在编写一个小的 mozilla 插件,但我在两个课程上遇到了同样的问题。

var { ActionButton } = require('sdk/ui/button/action');

var MateButton = function(mate)
{
    var self = this;
    myMate: mate,
    button: ActionButton(
    {
        id: "my-button",
        label: self.myMate.message_OFF,
        icon:
        {
            "16": self.myMate.icon_OFF,
        }
    }),
    onChange: function()
    {
        var mate = self.myMate;
        var icon_tmp = ((mate.online == true) ? mate.icon_ON : mate.icon_OFF);
        var message_tmp = ((mate.online == true) ? mate.message_ON : mate.message_OFF);

        self.button.state("window",
        {
            "label": message_tmp,
            "icon":
             {
                "16": icon_tmp,
            }
        });
    }
};

exports.MateButton = MateButton;

问题:

控制台在“onChange:function()”之前发现错误:SyntaxError:missing ;声明之前。

我尝试将“,”替换为“;”但错误变为“函数语句需要名称”。

之前我也试过删除函数onChange和冒号,但是错误转移到了按钮定义上。

谁能帮帮我?

【问题讨论】:

  • 删除,这里"16": icon,
  • myMate: mate; ,替换;
  • 是的,谢谢,我更改了“this.myMate = mate;”通过“我的伴侣:伴侣;”并忘记更改“;”。但它之前已经犯了这些错误0
  • 这一切都搞混了。你一半使用函数声明,一半使用对象声明。
  • 是的,没错。这是因为我需要一些对象和一种方法。

标签: javascript firefox-addon


【解决方案1】:

您的声明语法在这里混淆了。在您的部分代码中,您使用的是函数声明,而在另一部分中,您正在使用对象声明。

函数声明涉及运行一段代码并返回单个值,而对象声明不运行任何代码,而是返回(隐式)一系列键值对,(key: value,以,分隔)。

您的MateButton = function(mate) 行声明MateButton 是一个函数,因此key: value 对不合适。试试这个:

var MateButton = function(mate) {
  var self = this;
  self.myMate = mate;
  self.button = ActionButton({
    id: "my-button",
    label: self.myMate.message_OFF,
    icon: {
      "16": self.myMate.icon_OFF,
    }
  });
  self.onChange = function() {
    var mate = self.myMate;
    var icon_tmp = ((mate.online == true) ? mate.icon_ON : mate.icon_OFF);
    var message_tmp = ((mate.online == true) ? mate.message_ON : mate.message_OFF);

    self.button.state("window", {
      "label": message_tmp,
      "icon": {
        "16": icon_tmp,
      }
    });
  };
  return self;
};

【讨论】:

    猜你喜欢
    • 2017-03-25
    • 2016-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多