Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement inheritance in node.js modules?

Tags:

node.js

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?

like image 580
Yalamber Avatar asked Apr 25 '13 11:04

Yalamber


People also ask

How do I get inheritance in node JS?

3.0) method is an inbuilt application programming interface of the util module in which the constructor inherits the prototype methods from one to another and the prototype of that constructor is set to a new object which is created from superConstructor.

Does Nodejs support inheritance?

However keep in mind that you can write plain JavaScript to achieve inheritance in Node as well. You can use Object. create() to inherit one object to another in Node. js also.

How inheritance can be achieved using functions in Node JS?

JavaScript inheritance is done through prototypes. You do not define anything with a class keyword, but you make a function that's used as a constructor to build new objects (with the new keyword ).

How is inheritance implemented in JavaScript?

When it comes to inheritance, JavaScript only has one construct: objects. Each object has a private property which holds a link to another object called its prototype. That prototype object has a prototype of its own, and so on until an object is reached with null as its prototype.


3 Answers

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;
like image 191
fardjad Avatar answered Sep 29 '22 15:09

fardjad


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');
like image 25
cmac Avatar answered Sep 29 '22 17:09

cmac


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
        }
    });
};
like image 9
Ivan V. Avatar answered Sep 29 '22 16:09

Ivan V.