0

我正在尝试拥有一个具有一些常用方法(保存、更新等)的Base对象。我尝试过使用Object.create(Base),但无法弄清楚如何使用它module.exports

我如何将 Base 指定为 User 的原型?还是有更好的方法来实现我的目标?

模型/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 || '';    
};

模型/base.js

var Base = module.exports = function() {

};

Base.prototype.save = function() {
  // Some code for saving to DB
};
4

1 回答 1

1

尝试这个:

User.prototype = new Base();

不是在我现在可以测试的机器上,但理论上应该可以。

于 2012-09-10T19:29:39.237 回答