开发者

NodeJS Module.Exports Object Prototype Problem

开发者 https://www.devze.com 2023-03-14 15:26 出处:网络
I\'m just barely getting into NodeJS a bit and have hit a snag trying to create a (VERY)basic MVC implementation for it.

I'm just barely getting into NodeJS a bit and have hit a snag trying to create a (VERY)basic MVC implementation for it.

It comes down to the following. I have a main object for a Controller that I'm trying to create a prototype for; The code as follows:

var Controller = function(obj) {

    this.request = null;
    this.response = null;
    this.params = null;
    this.layout = 'default';    

}

Controller.prototype = new function() {

    this.processAction = function(action) {
        console.log("Processing Action.");
    }

}

module.exports = new Controller();

I've stripped most of the values / functions out for this problem as they don't really relate. Basically from my understanding using the module.exports will export the object to a variable using the require() function. I have the following in my dispatcher:

var Controller = require('./Controller.js');

The problem is whenever I printout the variable Controller I get the first part of the object but the prototype has not been included. See the following printout:

{ request: null,
  response: null,
  params: null,
  layout: 'default' }

Thus calling the prototype function Controller.processAction() results in a no method error. Am开发者_开发问答 I declaring this prototype wrong or is there something I'm missing related to NodeJS?

[EDIT]

I've also tried the following style for adding a prototype to no avail.

Controller.prototype = {
    'processAction' : function(action) {
        console.log("Processing Action");
    }
}

[EDIT 2]

Nevermind, the above worked console.log doesn't report the additional functionality in the prototype, interesting.


Controller.prototype = {
    processAction : function(){
        // code
    },

    anotherMethod : function(){
    }
}


use:

Controller.prototype = {
    processAction : function(action) {
        console.log("Processing Action.");
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消