【问题标题】:Browser support for class syntax in Javascript [duplicate]浏览器支持 Javascript 中的类语法 [重复]
【发布时间】:2016-03-19 09:59:55
【问题描述】:

下面的语法,

class Polygon {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
}


var Polygon = class {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
};



var Polygon = class Polygon {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
};

--

chrome 和 firefox 都不支持。

SyntaxError: Unexpected token class(…)

chrome 版本安装47.0.2526.80 m

已安装firefox版本44.0a2 (2015-12-12)

支持classextends 关键字的浏览器版本是什么?

【问题讨论】:

  • 为什么不使用受支持的东西,function Polygon
  • @SpencerWieczorek 基于类的范式看起来更容易(接近现实世界)构建抽象
  • 我认为您需要开启严格模式才能在 Chrome 上运行。

标签: javascript google-chrome firefox browser ecmascript-6


【解决方案1】:

【讨论】:

  • 任何想法,浏览器(跨)支持class 语法需要多长时间?
  • 真正的问题是——要多久才能有足够多的人拥有支持class 语法的浏览器。
  • 我正在使用 chrome 版本 47.0.2526.80 m 。这是否支持class 语法?
  • 可以,但仅限于严格模式。
  • 使用“dsoop.js”。它使用类语法,加上更多,然后在幕后进行转换,使其与每个浏览器兼容!
【解决方案2】:
  1. 您可以使用 javascript to javascript 编译器(例如 Babel)将 ES6 javascript 编译为 ES5 代码。它涵盖了大部分 ES6 功能。

  2. 查看https://kangax.github.io/compat-table/es6/ 了解 ES6 功能表以及不同浏览器对它们的支持程度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-11
    • 2011-03-11
    • 1970-01-01
    • 2011-02-20
    • 2013-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多