Javascript es6模块导出/导入:未捕获类型错误:无法读取属性';默认值';未定义的 问题描述:
我正在GameService类构造函数中创建一个新的英雄实例,如下所示:Javascript es6模块导出/导入:未捕获类型错误:无法读取属性';默认值';未定义的 问题描述:,javascript,ecmascript-6,Javascript,Ecmascript 6,我正在GameService类构造函数中创建一个新的英雄实例,如下所示: let image = Util.createImage('assets/images/head_blue.jpg'); this.hero = new Hero({ id: 1, name: 'test', x: 0, y: 0, width: 100, height: 100, bitmap: new createjs.Bitmap(image) }); 我已经添加了从“../entiti
let image = Util.createImage('assets/images/head_blue.jpg');
this.hero = new Hero({
id: 1,
name: 'test',
x: 0,
y: 0,
width: 100,
height: 100,
bitmap: new createjs.Bitmap(image)
});
我已经添加了从“../entities/Hero”导入Hero的import代码>在我的游戏服务中。linter jshint没有指出任何问题,当我通过gulp构建js文件时,没有显示任何错误。如果我输入一些随机的模块名,那么gulp将抛出一个错误
看起来一切正常,但我的浏览器中出现以下错误:
未捕获的TypeError:无法读取未定义的属性“default”
这个错误似乎直接来自我的对象类。如果我完全从Hero
类中注释Object
,错误就会消失
问题:
为什么会导出默认对象代码>导致此问题的原因是什么
代码
类:英雄扩展对象,
位于:/entities/hero.js
import Object from "./object";
class Hero extends Object{
constructor(options){
if(options === undefined){
throw new TypeError("Options not defined in Hero.");
}
super(options.x, options.y, options.width, options.height, options.bitmap);
this._id = options.id;
this._name = options.name;
}
set id(id){
this._id = id;
}
get id(){
return this._id;
}
set name(name){
this._name = name;
}
get name(){
return this._name;
}
}
export default Hero;
class Object{
constructor(x, y, width, height, bitmap){
this._x = x;
this._y = y;
this._width = width;
this._height = height;
this._bitmap = bitmap;
}
set x(x){
this._x = this._bitmap.x = x;
}
get x(){
return this._x;
}
set y(y){
this._y = this._bitmap.y = y;
}
get y(){
return this._y;
}
set width(width){
this._width = width;
}
get width(){
return this._width;
}
set height(height){
this._height = height;
}
get height(){
return this._height;
}
set bitmap(bitmap){
this._bitmap = bitmap;
}
get bitmap(){
return this._bitmap;
}
}
export default Object;
类:对象,
位于/entities/object.js
import Object from "./object";
class Hero extends Object{
constructor(options){
if(options === undefined){
throw new TypeError("Options not defined in Hero.");
}
super(options.x, options.y, options.width, options.height, options.bitmap);
this._id = options.id;
this._name = options.name;
}
set id(id){
this._id = id;
}
get id(){
return this._id;
}
set name(name){
this._name = name;
}
get name(){
return this._name;
}
}
export default Hero;
class Object{
constructor(x, y, width, height, bitmap){
this._x = x;
this._y = y;
this._width = width;
this._height = height;
this._bitmap = bitmap;
}
set x(x){
this._x = this._bitmap.x = x;
}
get x(){
return this._x;
}
set y(y){
this._y = this._bitmap.y = y;
}
get y(){
return this._y;
}
set width(width){
this._width = width;
}
get width(){
return this._width;
}
set height(height){
this._height = height;
}
get height(){
return this._height;
}
set bitmap(bitmap){
this._bitmap = bitmap;
}
get bitmap(){
return this._bitmap;
}
}
export default Object;
文件夹结构:
当我做this.hero=newhero({/*这里的东西*/})代码>我在game.service.js文件中(又名:GameService类)
已经是基类-
他提到这可能是我的transpiler中的一个bug,它应该能工作。现在,我刚刚将Object
重命名为BaseObject
我不知道,但是Object
对于基类来说是个可怕的名字-已经是基类了。@Bergi,这就是问题所在。我将它重命名为BaseObject,它可以正常工作。谢谢。很高兴它能工作。尽管如此,我还是认为你应该用你的transpiler报告它是一个bug,因为它应该是有效的。