简体   繁体   中英

NodeJS Module.Exports Object Prototype Problem

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.");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM