【发布时间】:2012-09-10 19:15:05
【问题描述】:
我正在尝试拥有一个具有一些常用方法(保存、更新等)的 Base 对象。我试过使用Object.create(Base),但不知道如何让它与module.exports一起工作。
如何将 Base 指定为 User 的原型?还是有更好的方法来实现我的目标?
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
};
【问题讨论】:
标签: javascript prototype appcelerator commonjs