节点。有需要的js ES6类

时间:2022-07-17 19:40:55

So up until now, i have created classes and modules in node.js the following way:

到目前为止,我已经在node中创建了类和模块。js以下方式:

    var fs = require('fs');

var animalModule = (function () {
    /**
     * Constructor initialize object
     * @constructor
     */
    var Animal = function (name) {
        this.name = name;
    };

    Animal.prototype.print = function () {
        console.log('Name is :'+ this.name);
    };

    return {
        Animal: Animal
    }
}());

module.exports = animalModule;

Now with ES6, you are able to make "actual" classes just like this:

现在有了ES6,您就可以像这样创建“实际”类:

class Animal{

 constructor(name){
    this.name = name ;
 }

 print(){
    console.log('Name is :'+ this.name);
 }
}

Now, first of all, i love this :) but it raises a question. How do you use this combined with node.js's module structure?

现在,首先,我喜欢这个:)但它提出了一个问题。如何与node结合使用。js的模块结构?

Say you have a class where you wish to use a module for the sake of demonstration say you wish to use fs

假设您有一个类,您希望在其中使用一个模块来进行演示,那么假设您希望使用fs

so you create your file:

创建文件:


Animal.js

Animal.js

var fs = require('fs');
class Animal{

 constructor(name){
    this.name = name ;
 }

 print(){
    console.log('Name is :'+ this.name);
 }
}

Would this be the right way?

这条路对吗?

Also, how do you expose this class to other files within my node project? And would you still be able to extend this class if you're using it in a separate file?

另外,如何将这个类公开给我的node项目中的其他文件?如果你在一个单独的文件中使用这个类,你还能扩展它吗?

I hope some of you will be able to answer these questions :)

我希望你们中的一些人能够回答这些问题。

3 个解决方案

#1


55  

Yes, your example would work fine.

是的,你的例子很有用。

As for exposing your classes, you can export a class just like anything else:

至于公开类,可以像导出其他类一样导出类:

class Animal {...}
module.exports = Animal;

Or the shorter:

或较短:

module.exports = class Animal {

};

Once imported into another module, then you can treat it as if it were defined in that file:

导入到另一个模块之后,您可以将其视为在该文件中定义的:

var Animal = require('./Animal');

class Cat extends Animal {
    ...
}

#2


2  

The ES6 way of require is import. You can export your class and import it somewhere else using import { ClassName } from 'path/to/ClassName'syntax.

ES6的需求方式是导入。您可以从“路径/到/ClassName”语法中导出类并使用import {ClassName}将其导入到其他地方。

import fs from 'fs';
export default class Animal {

  constructor(name){
    this.name = name ;
  }

  print(){
    console.log('Name is :'+ this.name);
  }
}

import Animal from 'path/to/Animal.js';

#3


2  

Just treat the ES6 class name the same as you would have treated the constructor name in the ES5 way. They are one and the same.

将ES6类名处理为与以ES5方式处理构造函数名相同。他们是一样的。

The ES6 syntax is just syntactic sugar and creates exactly the same underlying prototype, constructor function and objects.

ES6语法只是语法的糖块,并创建完全相同的底层原型、构造函数和对象。

So, in your ES6 example with:

在你的ES6例子中

class Animal {
    ...
}

var a = new Animal();

module.exports = {Animal: Animal};

You can just treat Animal like the constructor of your object (the same as you would have done in ES5). You can export the constructor. You can call the constructor with new Animal(). Everything is the same for using it. Only the declaration syntax is different. There's even still an Animal.prototype that has all your methods on it. The ES6 way really does create the same coding result, just with fancier/nicer syntax.

您可以像对待对象的构造函数一样对待动物(就像在ES5中所做的那样)。可以导出构造函数。可以使用new Animal()调用构造函数。使用它的一切都是一样的。只有声明语法不同。甚至还有一种动物。有你所有方法的原型。ES6方法确实创建了相同的编码结果,只是使用了更漂亮的语法。

#1


55  

Yes, your example would work fine.

是的,你的例子很有用。

As for exposing your classes, you can export a class just like anything else:

至于公开类,可以像导出其他类一样导出类:

class Animal {...}
module.exports = Animal;

Or the shorter:

或较短:

module.exports = class Animal {

};

Once imported into another module, then you can treat it as if it were defined in that file:

导入到另一个模块之后,您可以将其视为在该文件中定义的:

var Animal = require('./Animal');

class Cat extends Animal {
    ...
}

#2


2  

The ES6 way of require is import. You can export your class and import it somewhere else using import { ClassName } from 'path/to/ClassName'syntax.

ES6的需求方式是导入。您可以从“路径/到/ClassName”语法中导出类并使用import {ClassName}将其导入到其他地方。

import fs from 'fs';
export default class Animal {

  constructor(name){
    this.name = name ;
  }

  print(){
    console.log('Name is :'+ this.name);
  }
}

import Animal from 'path/to/Animal.js';

#3


2  

Just treat the ES6 class name the same as you would have treated the constructor name in the ES5 way. They are one and the same.

将ES6类名处理为与以ES5方式处理构造函数名相同。他们是一样的。

The ES6 syntax is just syntactic sugar and creates exactly the same underlying prototype, constructor function and objects.

ES6语法只是语法的糖块,并创建完全相同的底层原型、构造函数和对象。

So, in your ES6 example with:

在你的ES6例子中

class Animal {
    ...
}

var a = new Animal();

module.exports = {Animal: Animal};

You can just treat Animal like the constructor of your object (the same as you would have done in ES5). You can export the constructor. You can call the constructor with new Animal(). Everything is the same for using it. Only the declaration syntax is different. There's even still an Animal.prototype that has all your methods on it. The ES6 way really does create the same coding result, just with fancier/nicer syntax.

您可以像对待对象的构造函数一样对待动物(就像在ES5中所做的那样)。可以导出构造函数。可以使用new Animal()调用构造函数。使用它的一切都是一样的。只有声明语法不同。甚至还有一种动物。有你所有方法的原型。ES6方法确实创建了相同的编码结果,只是使用了更漂亮的语法。