Javascript 如何导出没有花括号的扩展类?(节点JS)

Javascript 如何导出没有花括号的扩展类?(节点JS),javascript,node.js,class,import,export,Javascript,Node.js,Class,Import,Export,我的目标是导出一个名为entry的扩展类,而不使用花括号(比如子类) 问题是,当我在子类上使用default关键字时,我无法访问该子类,浏览器会给我以下错误: 未捕获的语法错误:“默认”的重复导出 代码: 这种结构最初是不允许的吗?还是我在代码中遗漏了什么 感谢收听。显示错误是因为您将终端类和入口类作为默认导出 如果您只需要terminal.js中的入口类,请从terminal类中删除导出默认值。显示错误,因为您同时将终端类和入口类导出为默认值 如果您只需要terminal.js中的入口类,请从

我的目标是导出一个名为
entry
的扩展类,而不使用花括号(比如
子类

问题是,当我在
子类上使用
default
关键字时,我无法访问该子类,浏览器会给我以下错误:

未捕获的语法错误:“默认”的重复导出

代码:

这种结构最初是不允许的吗?还是我在代码中遗漏了什么


感谢收听。

显示错误是因为您将终端类和入口类作为默认导出


如果您只需要terminal.js中的入口类,请从terminal类中删除导出默认值。

显示错误,因为您同时将终端类和入口类导出为默认值


如果您只需要terminal.js中的入口类,请从terminal类中删除导出默认值。

我将您的项目调整为Node.js,因为这样更易于测试。在浏览器中,您仍然可以使用.js扩展名,但需要将文件作为模块引用,而不是作为脚本引用

对于Node.js,将扩展名.mjs用于ECMAScript模块(“导入”和“导出”是ECMAScript模块的一项功能)。 您只需要导出外部引用的标识符:(入口类)

使用“node--experimental modules./human.mjs”运行它

// terminal.mjs: only 'Entrance' is exported
// no need to export 'Terminal' as long as it's name is not referenced outside
class Terminal {
    constructor(output) {
        this.output = output;
        console.log(`This is Terminal`);
    }
}
export default class Entrance extends Terminal {
    constructor(output) {
        super(output);
    }
    ticket() {
        console.log(`Ticket please`);
    }
}

// human.mjs
import Entrance from './terminal.mjs';
class People {
    constructor(name, output) {
        this.name = name;
        this.input = new Entrance(this);
        this.input.ticket();
    }
}
const guy = new People('james');
如果还希望引用外部的Terminal类,请不要使用“默认”导出/导入(或创建顶层对象,将Terminal和Entry作为成员):

现在使用默认导出,但封装到库对象中。这可能是执行此操作的标准方法,但仅导出您在外部引用的符号:

// terminal4.mjs: using a top level object and a default export
class Terminal {
    constructor(output) {
        this.output = output;
        console.log(`This is Terminal`);
    }
}
class Entrance extends Terminal {
    constructor(output) {
        super(output);
    }
    ticket() {
        console.log(`Ticket please`);
    }
}
const myLib = {Terminal, Entrance};
export default myLib;
// or just: export default {Terminal, Entrance};'

// human4.mjs
import term from './terminal4.mjs';
class People {
    constructor(name, output) {
        this.name = name;
        this.input = new term.Entrance(this);
        this.input.ticket();
        new term.Terminal(this);
    }
}
const guy = new People('james');
参考资料:

  • “出口”:
  • “导入”:
  • Node.js ECMAScript模块:

我将您的项目调整为Node.js,因为这更易于测试。在浏览器中,您仍然可以使用.js扩展名,但需要将文件作为模块引用,而不是作为脚本引用

对于Node.js,将扩展名.mjs用于ECMAScript模块(“导入”和“导出”是ECMAScript模块的一项功能)。 您只需要导出外部引用的标识符:(入口类)

使用“node--experimental modules./human.mjs”运行它

// terminal.mjs: only 'Entrance' is exported
// no need to export 'Terminal' as long as it's name is not referenced outside
class Terminal {
    constructor(output) {
        this.output = output;
        console.log(`This is Terminal`);
    }
}
export default class Entrance extends Terminal {
    constructor(output) {
        super(output);
    }
    ticket() {
        console.log(`Ticket please`);
    }
}

// human.mjs
import Entrance from './terminal.mjs';
class People {
    constructor(name, output) {
        this.name = name;
        this.input = new Entrance(this);
        this.input.ticket();
    }
}
const guy = new People('james');
如果还希望引用外部的Terminal类,请不要使用“默认”导出/导入(或创建顶层对象,将Terminal和Entry作为成员):

现在使用默认导出,但封装到库对象中。这可能是执行此操作的标准方法,但仅导出您在外部引用的符号:

// terminal4.mjs: using a top level object and a default export
class Terminal {
    constructor(output) {
        this.output = output;
        console.log(`This is Terminal`);
    }
}
class Entrance extends Terminal {
    constructor(output) {
        super(output);
    }
    ticket() {
        console.log(`Ticket please`);
    }
}
const myLib = {Terminal, Entrance};
export default myLib;
// or just: export default {Terminal, Entrance};'

// human4.mjs
import term from './terminal4.mjs';
class People {
    constructor(name, output) {
        this.name = name;
        this.input = new term.Entrance(this);
        this.input.ticket();
        new term.Terminal(this);
    }
}
const guy = new People('james');
参考资料:

  • “出口”:
  • “导入”:
  • Node.js ECMAScript模块:

这是可行的,但是没有办法导入3个以上没有花括号的类吗?每个文件只允许一个默认导出。因此,您必须通过花括号导入其他内容。这里的更多细节:这是可行的,但是没有办法导入不带花括号的3个以上的类吗?每个文件只允许一个默认导出。因此,您必须通过花括号导入其他内容。这里的更多细节:也许这是一个愚蠢的问题,但是关键字
term
是否是某种神奇的值,比如JS中的
默认值
事件
e
?还是一个普通的词?使用
.mjs
键入和
*
对我来说是全新的体验,所以我想知道得更清楚一些,即使这是一件小事。“term”不是关键词,它只是一个随机选择的标识符,你可以在这里使用任何名称。也许这是一个愚蠢的问题,但是,在JS中,关键字
term
是否是某种神奇的值,比如
默认值
事件
e
?还是一个普通的词?使用
.mjs
键入和
*
对我来说是全新的体验,所以我想知道得更清楚一些,即使这是一件小事。“term”不是关键字,它只是随机选择的标识符,你可以在这里使用任何名称。