Javascript 如何在 Node 4 中正确导出 ES6 类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32657516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to properly export an ES6 class in Node 4?
提问by Jér?me Verstrynge
I defined a class in a module:
我在模块中定义了一个类:
"use strict";
var AspectTypeModule = function() {};
module.exports = AspectTypeModule;
var AspectType = class AspectType {
// ...
};
module.export.AspectType = AspectType;
But I get the following error message:
但我收到以下错误消息:
TypeError: Cannot set property 'AspectType' of undefined
at Object.<anonymous> (...\AspectType.js:30:26)
at Module._compile (module.js:434:26)
....
How should I export this class and use it in another module? I have seen other SO questions, but I get other error messages when I try to implement their solutions.
我应该如何导出这个类并在另一个模块中使用它?我看过其他 SO 问题,但是当我尝试实施他们的解决方案时,我收到了其他错误消息。
回答by loganfsmyth
If you are using ES6 in Node 4, you cannot use ES6 module syntax without a transpiler, but CommonJS modules (Node's standard modules) work the same.
如果您在 Node 4 中使用 ES6,则不能在没有转译器的情况下使用 ES6 模块语法,但 CommonJS 模块(Node 的标准模块)的工作方式相同。
module.export.AspectType
should be
应该
module.exports.AspectType
hence the error message "Cannot set property 'AspectType' of undefined" because module.export === undefined
.
因此错误消息“无法设置未定义的属性‘AspectType’”,因为module.export === undefined
.
Also, for
另外,对于
var AspectType = class AspectType {
// ...
};
can you just write
你能写吗
class AspectType {
// ...
}
and get essentially the same behavior.
并获得基本相同的行为。
回答by sitrakay
// person.js
'use strict';
module.exports = class Person {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
display() {
console.log(this.firstName + " " + this.lastName);
}
}
// index.js
'use strict';
var Person = require('./person.js');
var someone = new Person("First name", "Last name");
someone.display();
回答by Jonas Brandel
With ECMAScript 2015 you can export and import multiple classes like this
使用 ECMAScript 2015,您可以像这样导出和导入多个类
class Person
{
constructor()
{
this.type = "Person";
}
}
class Animal{
constructor()
{
this.type = "Animal";
}
}
module.exports = {
Person,
Animal
};
then where you use them:
那么你在哪里使用它们:
const { Animal, Person } = require("classes");
const animal = new Animal();
const person = new Person();
In case of name collisions, or you prefer other names you can rename them like this:
在名称冲突的情况下,或者您更喜欢其他名称,您可以像这样重命名它们:
const { Animal : OtherAnimal, Person : OtherPerson} = require("./classes");
const animal = new OtherAnimal();
const person = new OtherPerson();
回答by Thank you
Use
用
// aspect-type.js
class AspectType {
}
export default AspectType;
Then to import it
然后导入
// some-other-file.js
import AspectType from './aspect-type';
Read http://babeljs.io/docs/learn-es2015/#modulesfor more details
回答by masakielastic
class expressioncan be used for simplicity.
为简单起见,可以使用类表达式。
// Foo.js
'use strict';
// export default class Foo {}
module.exports = class Foo {}
-
——
// main.js
'use strict';
const Foo = require('./Foo.js');
let Bar = new class extends Foo {
constructor() {
super();
this.name = 'bar';
}
}
console.log(Bar.name);
回答by Behnam Ghiaseddin
I simply write it this way
我只是这样写
in the AspectType file:
在 AspectType 文件中:
class AspectType {
//blah blah
}
module.exports = AspectType;
and import it like this:
并像这样导入它:
const AspectType = require('./AspectType');
var aspectType = new AspectType;
回答by Crates
Several of the other answers come close, but honestly, I think you're better off going with the cleanest, simplest syntax. The OP requested a means of exporting a class in ES6 / ES2015. I don't think you can get much cleaner than this:
其他几个答案很接近,但老实说,我认为你最好使用最干净、最简单的语法。OP 要求一种在 ES6/ES2015 中导出类的方法。我认为你不能比这更干净:
'use strict';
export default class ClassName {
constructor () {
}
}
回答by Jelmer Jellema
Sometimes I need to declare multiple classes in one file, or I want to export base classes and keep their names exported because of my JetBrains editor understands that better. I just use
有时我需要在一个文件中声明多个类,或者我想导出基类并保留它们的名称,因为我的 JetBrains 编辑器更了解这一点。我只是用
global.MyClass = class MyClass { ... };
And somewhere else:
还有其他地方:
require('baseclasses.js');
class MySubclass extends MyClass() { ... }
回答by shahar taite
I had the same problem. What i found was i called my recieving object the same name as the class name. example:
我有同样的问题。我发现我将我的接收对象称为与类名相同的名称。例子:
const AspectType = new AspectType();
this screwed things up that way... hope this helps
这把事情搞砸了……希望这会有所帮助