【 在 saynothing (止语) 的大作中提到: 】
: // Base.js
: module.exports = function(db) {
: this.db = db;
: };
: module.exports.prototype = {
: extend: function(properties) {
: var Child = module.exports;
does this code actually work??? Child is not a new function but the
original function, and code below just mutates the original function
: Child.prototype = module.exports.prototype; // [1]
: for(var key in properties) {
: Child.prototype[key] = properties[key];
: }
: return Child;
: },
: setDB: function(db) {
: this.db = db;
: },
: collection: function() {
: if(this._collection) return this._collection;
: return this._collection = this.db.collection('fastdelivery-
: content');
: }
: }
: // 这么写的好处是,可以:var model = new (require "Base"); 并且预留接口便于
: 定义派生对象
: 下面这段代码,看的我晕了半天。虽然我知道是定义一个派生对象,派生对象有
: insert, update, getlist, remove这些方法。
: // ContentModel.js
: var Model = require("./Base"),
: crypto = require("crypto"),
: model = new Model();
: var ContentModel = model.extend({ // [2]
: insert: function(data, callback) {
: data.ID = crypto.randomBytes(20).toString('hex');
: this.collection().insert(data, {}, callback || function(){ });
: },
: update: function(data, callback) {
: this.collection().update({ID: data.ID}, data, {}, callback ||
: function(){ });
: },
: getlist: function(callback, query) {
: this.collection().find(query || {}).toArray(callback);
: },
: remove: function(ID, callback) {
: this.collection().findAndModify({ID: ID}, [], {}, {remove: true},
: callback);
: }
: });
: module.exports = ContentModel; // [3]
: 问题:
: [1] 这行代码是否可以省略?
: [2] extend方法什么时候执行? 先于[3]语句,还是后于[3]语句。 执行extend方法
: 时, module.exports该指向谁?
--
FROM 183.195.251.*