I’m trying to have a Base object that has some common methods (save, update, etc). I’ve tried using Object.create(Base), but could not figure out how to get it to work with module.exports.
How would I assign Base as the prototype of User? Or is there a better way to accomplish my goals?
models/user.js
var Base = require('models/base.js');
var User = module.exports = function(data) {
data = data || {};
this.fname = data.fname || '';
this.lname = data.lname || '';
this.email = data.email || '';
};
/**
* Takes a response from FB and convert it to object
*/
User.prototype.importFacebookData = function(result) {
this.fname = result.first_name || '';
this.lname = result.last_name || '';
this.name = result.name || this.fname + ' ' + this.lname || '';
this.email = result.email || '';
};
models/base.js
var Base = module.exports = function() {
};
Base.prototype.save = function() {
// Some code for saving to DB
};
Try this:
Not at a machine I can test it on right now, but theoretically that should work.