35

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.

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?

4 Answers 4

45

in 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_model becomes:

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;
Sign up to request clarification or add additional context in comments.

9 Comments

Can we use utils.inherits in this?
@askkirati Yes, see the updated answer. util.inherits does something very similar to what I've done, it's just my preference to use Object.create() directly.
Hello, how should i access fromID from where i have loaded the UserModel modeule? if i do var user_model = require('./UserModel'); then doing user_model.fromID(); gives undefined user_model to me
you should do var userModel = new require('./UserModel');
public methods are not exposed. What's the point of inheritance if you have to manually define them each time?
|
28

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

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');

Comments

9

Using utility.inherits can also help you decouple the child from the parent.

Instead of calling the parent explicitly, you can use super_ to call the parent.

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.inherits source:

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

1 Comment

Please, note, that if you use util.inherits you should use UserModel.super_.apply(this, arguments); instead of this.super_.apply(this, arguments);
0

New to NodeJS dev, and I came here looking for a similar answer. Based on the accepted answer, I think a possible alternative is to simply not do it. That code looks painful. Maybe there are better ways to do it now, but sometimes in software the answer is to think differently.

The JS ecosystem seems to be predicated on composition paradigms, rather than inheritance. So instead of looking to have user.js inherit from base, it would be more clean to simply import the functionality you need to support the code-reuse. Or, move to dotnet core. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.