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 es 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.
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 es 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.
Share Improve this question edited Jun 22, 2011 at 22:21 Aric asked Jun 22, 2011 at 22:13 AricAric 231 silver badge4 bronze badges 3-
Your exact code works fine. Have you actually tried calling
.processAction()
or are you just assuming that since it doesn't show up in a log it doesn't exist? Some versions of node don't properly iterate through__proto__
– user578895 Commented Jun 22, 2011 at 22:21 - You're absolutely right. It didn't show up in the log so I assumed it wasn't there. Thanks for the help. – Aric Commented Jun 22, 2011 at 22:24
-
In case you don't know
new function()
returns an object not a function. – Raynos Commented Jun 23, 2011 at 7:34
2 Answers
Reset to default 4Controller.prototype = {
processAction : function(){
// code
},
anotherMethod : function(){
}
}
use:
Controller.prototype = {
processAction : function(action) {
console.log("Processing Action.");
}
}