How to implement inheritance in node.js modules? -


i in process of writing nodejs app. based on expressjs. confused on doing inheritance in nodejs modules. trying create model base class, let's my_model.js.

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

now want use methods in my_model in other model class. let's user_model.js how inherit my_model in user_model?

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, can use util.inherits, 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; 

Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -