簡體   English   中英

NodeJS Module.Exports對象原型問題

[英]NodeJS Module.Exports Object Prototype Problem

我只是勉強進入NodeJS,遇到了嘗試為其創建(VERY)基本MVC實現的障礙。

歸結為以下幾點。 我有一個要創建其原型的Controller的主要對象; 代碼如下:

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();

我已經針對該問題去除了大部分的值/函數,因為它們實際上並不相關。 基本上從我的理解來看,使用module.exports將使用require()函數將對象導出到變量。 我的調度員中有以下內容:

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

問題是,每當我打印出變量Controller時,我都會得到對象的第一部分,但原型並未包括在內。 請參閱以下打印輸出:

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

因此,調用原型函數Controller.processAction()會導致無方法錯誤。 我是在聲明這個原型錯誤還是我缺少與NodeJS相關的東西?

[編輯]

我還嘗試了以下樣式,以添加原型無效。

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

[編輯2]

沒關系,上面工作的console.log並沒有報告原型中的其他功能,很有意思。

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

    anotherMethod : function(){
    }
}

采用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM