javascript 中的语法错误:缺少;声明前

Syntax error in javascript: missing ; before statement

我正在编写一个小的 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;声明之前。

我试过用“;”替换“,”但错误变为 "function statement requires a name".

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

谁能帮帮我?

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

函数声明涉及 运行 一段代码并返回 单个值 ,而对象声明 不运行code,取而代之的是returns(隐式)一系列键值对,(key: value,用[=分隔12=]).

您的 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;
};