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?
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.
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.
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 ).
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.
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;
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');
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
}
});
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With