【问题标题】:ES6 Class is not a function when imported via System通过 System 导入时,ES6 类不是函数
【发布时间】:2014-12-30 16:57:01
【问题描述】:

通过系统导入游戏怎么不是函数

import Core from 'gameUnits/Core' 

export class Game { 
constructor() {

核心:

export class Core {
    constructor(scene) {
    }
}

在浏览器中:

     <script src="bower_components/traceur/traceur.js"></script>
  <script src="bower_components/es6-module-loader/dist/es6-module-loader.js"></script>
  <script>
    System.import('Game').then(function (Game) {
      game = new Game();
    });
  </script>

【问题讨论】:

  • export default class Game {...} 可能更有用。它不是带有Game 属性的Module 类型对象,而是直接导出Game

标签: javascript ecmascript-6 ecmascript-harmony


【解决方案1】:

您的模块对象不是Game,它包含 Game。试试:

System.import('Game').then(function ({Game}) {
  game = new Game();
});

【讨论】:

  • {Game} 的真正含义是什么?
  • @SuperUberDuper,它解构参数对象,提取属性"Game"并将其绑定到同名变量。与命名参数x和绑定let Game = x.Game效果相同。
  • 这是es6的特性吗?
  • @SuperUberDuper,是的。
  • chrome 不喜欢的是:Uncaught SyntaxError: Unexpected token {
猜你喜欢
  • 1970-01-01
  • 2016-11-01
  • 2020-09-01
  • 2017-02-23
  • 2016-07-03
  • 2011-10-01
  • 1970-01-01
  • 2016-03-10
  • 2016-06-22
相关资源
最近更新 更多