Importing/Exporting ES6 中的原型
Importing/Exporting prototypes in ES6
上下文:
我目前正在使用 Phaser 框架开发游戏。我得到了一个可以使用的代码库,因此无法更改。 Phaser 没有 import/export 的原生方式,所以我使用 webpack 和 babel 来绕过 with lots of help from this template.
我的问题源于尝试导入和导出以下代码:
给出的代码:
AchievementManager = function(a) {
this.game = a,
this._playerDataModified = !1,
this.newLevels = [],
};
AchievementManager.prototype = {
_getTotalLoyaltyPoints: function() {
return 1;
}
}
我尝试导出。 Returns 编译错误(不在浏览器中)'Module build failed: SyntaxError: Unexpected token, expected {' 参考 AchievementManager:
export AchievementManager = function(a) {
this.game = a,
this._playerDataModified = !1,
this.newLevels = [],
};
export AchievementManager.prototype = {
_getTotalLoyaltyPoints: function() {
return 1;
}
}
如果有人能帮我找出如何 export/import 类 以这种形式编写的内容,同时保持原型功能等核心概念,那就太好了!
导出构造函数就够了。原型是它的 属性,您可以像那样访问它(如果您需要它 - 与导入的构造函数交互的通常方式是 new
它们)。您不必在 属性 作业上放置某种 "visibility annotation"。您当前的代码不起作用,因为只能在 ES6 中导出变量绑定。
你应该写
export function AchievementManager(a) {
this.game = a;
this._playerDataModified = false;
this.newLevels = [];
}
AchievementManager.prototype._getTotalLoyaltyPoints = function() {
return 1;
};
您已经导出 AchievementManager
。没有任何理由导出 AchievementManager.prototype
。导入时只需在 AchievementManager 上使用 .prototype
。
无论如何,如果你想:
AchievementManager.prototype = {
_getTotalLoyaltyPoints: function() {
return 1;
}
}
export const AchievementManagerPrototype = AchievementManager.prototype;
上下文:
我目前正在使用 Phaser 框架开发游戏。我得到了一个可以使用的代码库,因此无法更改。 Phaser 没有 import/export 的原生方式,所以我使用 webpack 和 babel 来绕过 with lots of help from this template.
我的问题源于尝试导入和导出以下代码:
给出的代码:
AchievementManager = function(a) {
this.game = a,
this._playerDataModified = !1,
this.newLevels = [],
};
AchievementManager.prototype = {
_getTotalLoyaltyPoints: function() {
return 1;
}
}
我尝试导出。 Returns 编译错误(不在浏览器中)'Module build failed: SyntaxError: Unexpected token, expected {' 参考 AchievementManager:
export AchievementManager = function(a) {
this.game = a,
this._playerDataModified = !1,
this.newLevels = [],
};
export AchievementManager.prototype = {
_getTotalLoyaltyPoints: function() {
return 1;
}
}
如果有人能帮我找出如何 export/import 类 以这种形式编写的内容,同时保持原型功能等核心概念,那就太好了!
导出构造函数就够了。原型是它的 属性,您可以像那样访问它(如果您需要它 - 与导入的构造函数交互的通常方式是 new
它们)。您不必在 属性 作业上放置某种 "visibility annotation"。您当前的代码不起作用,因为只能在 ES6 中导出变量绑定。
你应该写
export function AchievementManager(a) {
this.game = a;
this._playerDataModified = false;
this.newLevels = [];
}
AchievementManager.prototype._getTotalLoyaltyPoints = function() {
return 1;
};
您已经导出 AchievementManager
。没有任何理由导出 AchievementManager.prototype
。导入时只需在 AchievementManager 上使用 .prototype
。
无论如何,如果你想:
AchievementManager.prototype = {
_getTotalLoyaltyPoints: function() {
return 1;
}
}
export const AchievementManagerPrototype = AchievementManager.prototype;