簡體   English   中英

如何在Firefox中使用javascript類?

[英]How do I use javascript classes with Firefox?

如何在Firefox中使用Javascript編寫類代碼? 我正在將媒體存儲網站構建為一個項目,並且作為該項目的一部分,我創建了一個包含媒體元素的類。 生成的類定義代碼在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;
    }
}

瀏覽器正趕上ES6語法。 我認為Firefox 45版開始支持類語法。

如果您還希望ES6代碼也可以在舊的瀏覽器上運行,則必須使用babeljs或Google traceur將代碼轉換為ES5(稱為轉堆)。 然后在前端使用它。

您可以使用Babel REPL來查看如何將其轉換為es5代碼(firefox支持完整規范)並在您的應用程序上使用該代碼,但是我強烈建議您按照kra3的說明在代碼上使用一些編譯 BabelGoogle 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; }; 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM