如何在 node.js 模块中实现继承?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16213495/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:18:31  来源:igfitidea点击:

How to implement inheritance in node.js modules?

node.js

提问by Yalamber

I am in process of writing nodejs app. It is based on expressjs. I am confused on doing inheritance in nodejs modules. What i am trying to do is create a model base class, let's say my_model.js.

我正在编写 nodejs 应用程序。它基于 expressjs。我对在 nodejs 模块中进行继承感到困惑。我想要做的是创建一个模型基类,比如说 my_model.js。

module.exports = function my_model(){
  my_model.fromID = function(){
    //do query here
  }
}

Now i want to use those methods in my_model in my other model class. let's say user_model.js How do i inherit my_model in user_model?

现在我想在我的其他模型类的 my_model 中使用这些方法。假设 user_model.js 我如何在 user_model 中继承 my_model?

回答by fardjad

in base_model:

在 base_model 中:

function BaseModel() { /* ... */ }

BaseModel.prototype.fromID = function () { /* ... */ };

module.exports = BaseModel;

in user_model:

在用户模型中:

var BaseModel = require('relative/or/absolute/path/to/base_model');

function UserModel() {
    UserModel.super_.apply(this, arguments);
}

UserModel.super_ = BaseModel;

UserModel.prototype = Object.create(BaseModel.prototype, {
    constructor: {
        value: UserModel,
        enumerable: false
    }
});

UserModel.prototype.yourFunction = function () { /* ... */ };

module.exports = UserModel;


Instead of using Object.create()directly, you can also use util.inherits, so your user_modelbecomes:

除了Object.create()直接使用,您还可以使用util.inherits,这样您user_model就变成了:

var BaseModel = require('relative/or/absolute/path/to/base_model'),
    util = require('util');

function UserModel() {
    BaseModel.apply(this, arguments);
}

util.inherits(UserModel, BaseModel);

UserModel.prototype.yourFunction = function () { /* ... */ };

module.exports = UserModel;

回答by cmac

With ES6 the usage of util.inherits()is discouraged in favor of ES6 classand extends

在 ES6中不鼓励使用util.inherits()以支持 ES6扩展

const EventEmitter = require('events');

class MyStream extends EventEmitter {
  constructor() {
    super();
  }
  write(data) {
    this.emit('data', data);
  }
}

const stream = new MyStream();

stream.on('data', (data) => {
  console.log(`Received data: "${data}"`);
});
stream.write('With ES6');

回答by Ivan V.

Using utility.inheritscan also help you decouple the childfrom the parent.

使用utility.inherits还可以帮助你解耦childparent

Instead of calling the parentexplicitly, you can use super_to call the parent.

parent您可以使用super_来调用父对象,而不是显式调用 。

var BaseModel = require('relative/or/absolute/path/to/base_model'),
util = require('util');

function UserModel() {
   this.super_.apply(this, arguments);
}

util.inherits(UserModel, BaseModel);

utility.inheritssource:

utility.inherits来源:

var inherits = function (ctor, superCtor) {
ctor.super_ = superCtor;
ctor.prototype = Object.create(superCtor.prototype, {
    constructor: {
        value: ctor,
        enumerable: false
        }
    });
};