0

When my server receives a request, I create a dispatcher which creates a router and ask to my router the controller that my dispatcher needs, so I send a URL to my router and with an array with every path. My router sends the controller to my dispatcher and my dispatcher calls the controlle index() func...

So my problem is, when I initialize my controllers, my controllers are overwritten by the last controller. Could someone tell me why?

This is my router, I've created a small code with logs to show the problem:

function Router(){
    log(colors.green+"Router initialization...")
    this.indexController = new (require("../controllers/index"))();
    log(this.indexController.constructor.name);

    this.notFoundController = new (require("../controllers/notFound"))();
    log(this.notFoundController.constructor.name);

    this.publicController = new (require("../controllers/public"))();
    log(this.publicController.constructor.name);

    this.faviconController = new (require("../controllers/favicon"))();
    log(this.faviconController.constructor.name);

    this.registerController = new (require("../controllers/register"))();
    log(this.registerController.constructor.name);
    log(colors.green+"Router initialization successful")

    log(this.indexController.constructor.name);
    log(this.notFoundController.constructor.name);
    log(this.publicController.constructor.name);
    log(this.faviconController.constructor.name);
    log(this.registerController.constructor.name);
}

console logs :

Log(21:9:4): Router initialization...
Log(21:9:4): IndexController
Log(21:9:4): NotFoundController
Log(21:9:4): PublicController
Log(21:9:4): FaviconController
Log(21:9:4): RegisterController
Log(21:9:4): Router initialization successful
Log(21:9:4): RegisterController
Log(21:9:4): RegisterController
Log(21:9:4): RegisterController
Log(21:9:4): RegisterController
Log(21:9:4): RegisterController

Why are my variables overwritten?

register.js:

function RegisterController() {}
RegisterController.prototype = Controller.prototype;
RegisterController.prototype.constructor = RegisterController;
RegisterController.prototype.index = function(content) {
    if (content.req.method == "POST") {
        content.res.writeHeader(200, {
            "Content-Type": "text/html"
        });
        content.res.write(content.datas);
        content.res.end();
    } else {
        this.render.call(content, "register");
    }
}
module.exports = RegisterController;
Banou26
  • 129
  • 1
  • 12
  • Can you show us the contents of `controllers/register.js`, please? – Bergi Jan 17 '15 at 20:35
  • function RegisterController(){ } RegisterController.prototype = Controller.prototype; RegisterController.prototype.constructor = RegisterController; RegisterController.prototype.index = function(content){ if(content.req.method == "POST"){ content.res.writeHeader(200, {"Content-Type": "text/html"}); content.res.write(content.datas); content.res.end(); } else{ this.render.call(content, "register"); } } module.exports = RegisterController; – Banou26 Jan 17 '15 at 20:39
  • 2
    No, no, please [edit] your question instead of posting code in comments. Oh, and welcome to StackOverflow :-) – Bergi Jan 17 '15 at 20:41

1 Answers1

0

You are assigning Controller.prototype to RegisterController.protoype and then modifying it:

RegisterController.prototype = Controller.prototype;
RegisterController.prototype.constructor = RegisterController;

This will modify the prototype of every controller in your program.

I'm assuming that the .js files for all of your controllers are doing this, and the first half of your output just looks right because you're logging them one at a time. That would explain the output at the end, because they're all sharing the same constructor.

You need to make a copy of the prototype before you modify it:

RegisterController.prototype = Object.create(Controller.prototype);


As a side note, contrary to what you thought, the variables in the code you showed us were not being overwritten. If you had done this:
console.log(this.indexController === this.notFoundController);

You would have seen:

false
JLRishe
  • 99,490
  • 19
  • 131
  • 169