如何在Firefox中使用javascript类?

时间:2016-05-19 21:32:26

标签: javascript class firefox

如何在Javascript中编写适用于Firefox的类?我正在构建一个媒体存储网站作为项目,作为项目的一部分,我创建了一个将保存媒体元素的类。生成的类定义代码在Chrome中运行良好,但在Firefox中不起作用,它会触发"类是保留标识符"警告。一些谷歌搜索证实这是因为firefox不支持类。

我该如何解决这个问题?我猜测有一些解决方法,答案不仅仅是为Firefox使用无类别代码。

道歉(但也谢谢!)事先,可能是一个简单的问题。我是一名新的程序员。

如果有帮助,请点击该课程的代码:

class MediaElement {
    constructor(DemographicCategory,GenderCategory,Name,Link,Images,Blurbs,TypeofMedia,Summary,Rating){
        this.Name = Name;
        this.Link = Link;
        this.Image = Images;
        this.Blurbs = Blurbs;
        this.Summary = Summary;
        this.Rating = Rating;
    }
}

2 个答案:

答案 0 :(得分:1)

浏览器正在追赶ES6语法。 我认为Firefox 45版开始支持类语法。

如果您希望ES6代码也能在旧版浏览器上运行,则必须将代码转换为ES5 - 称为转换 - 使用babeljs或Google traceur。然后在前端使用它。

答案 1 :(得分:1)

您可以使用Babel REPL查看它如何转换为es5代码(firefox完全支持的规范)并在您的应用上使用该代码,但我强烈建议您在代码上使用某些转换器,如上所述kra3Babelgoogle Traceur做了非常出色的工作

这是你的课程在翻译后的样子



"use strict";

function _classCallCheck(instance, Constructor) {
  if (!(instance instanceof Constructor)) {
    throw new TypeError("Cannot call a class as a function");
  }
}

var MediaElement = function MediaElement(DemographicCategory, GenderCategory, Name, Link, Images, Blurbs, TypeofMedia, Summary, Rating) {
  _classCallCheck(this, MediaElement);

  this.Name = Name;
  this.Link = Link;
  this.Image = Images;
  this.Blurbs = Blurbs;
  this.Summary = Summary;
  this.Rating = Rating;
};