简体   繁体   English

NodeJS Module.Exports对象原型问题

[英]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. 我只是勉强进入NodeJS,遇到了尝试为其创建(VERY)基本MVC实现的障碍。

It comes down to the following. 归结为以下几点。 I have a main object for a Controller that I'm trying to create a prototype for; 我有一个要创建其原型的Controller的主要对象; 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. 基本上从我的理解来看,使用module.exports将使用require()函数将对象导出到变量。 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. 问题是,每当我打印出变量Controller时,我都会得到对象的第一部分,但原型并未包括在内。 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. 因此,调用原型函数Controller.processAction()会导致无方法错误。 Am I declaring this prototype wrong or is there something I'm missing related to NodeJS? 我是在声明这个原型错误还是我缺少与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] [编辑2]

Nevermind, the above worked console.log doesn't report the additional functionality in the prototype, interesting. 没关系,上面工作的console.log并没有报告原型中的其他功能,很有意思。

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

    anotherMethod : function(){
    }
}

use: 采用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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