Hello,

Inheritance in javascript does not work with classes as you've already 
discovered. It works with prototypes and constructors.

You may find the util.inherits [1] method useful as it mimics classical 
simple inheritance.

your code would then look something like that :

ServerController.js:
function ServerController() {
  Server.call(this); // this would seem odd not to call the parent 
constructor
}
util.inherits(ServerController, Server);
ServerController.prototype.test = function () { 
  // override
}


[1] 
http://nodejs.org/api/util.html#util_util_inherits_constructor_superconstructor

On Monday, 2 March 2015 05:15:09 UTC+1, Aaron Martone wrote:
>
> Sorry in advance, I've been at this problem for 6 days now and have failed 
> at every single turn on what I need to do. I simply cannot afford to waste 
> more time, so I'm asking for help.
>
> My directory structure looks like this:
>
> /root
> ____/controllers
> ________server.ctrl.js
> ____/libs
> ________/classes
> ____________Server.js
> ____________ServerController.js
> server.app.js
>
> I am trying to create a "Server" class based on /libs/classes/Server.js. 
> When instantiated, it takes 1 param, the constant ENV (environment), which 
> is stored in its THIS scope. 
> One of the properties of the "Server" object is called 'ctrl' and that 
> should be set to an instance of the 'ServerController' class at 
> /libs/classes/ServerController.js
>
> The 'ServerController' needs to inherit via the prototype chain, the 
> properties on the 'Server' so that it can gain access to the 'THIS' scope's 
> 'ENV' when it references 'this.ENV'.
> I have tried everything I can think of, many times 20x over, and cannot 
> figure out how this is done. Hopefully I have explained my situation well 
> enough. I'm able to do inheritance without modules, but something about 
> Node's module exporting is throwing me off my understanding.
>
> *FYI*. The ServerController 'class' requires the /controllers/server.ctrl 
> module which exports an object of functions that the ServerController class 
> returns as direct functions off itself. Since ServerController gets stored 
> in the Server object's 'ctrl' property, I was hoping I could call it via 
> Server.ctrl.functionName();
>
>
> *==================================================================*
> *FROM HERE BELOW ARE THINGS I'VE TRIED, NOT NECESSARY TO THE QUESTION*
> *==================================================================*
>
>
>
> *in server.app.js:*
> var Server = require('./libs/classes/Server'); // the 'Server' class.
> var ServerController = require('./libs/classes/ServerController'); // the 
> 'ServerController' class.
>
> var ctrl = new ServerController();
> var server = new Server(ENV, ctrl);
>
> *in Server.js*:
> function Server(ENV, ctrl) { this.ENV = ENV; this.ctrl = ctrl; }
> Server.prototype.test = function() { console.log('Server-level test'); }
> module.exports = Server;
>
> *in ServerController.js:*
> var Server = require('./Server');
> function ServerController() { }
> ServerController.prototype = Object.create(Server.prototype);
> ServerController.prototype.test = function() { 
> console.log('Controller-level test, shadows Server-level'); }
> module.exports = ServerController;
>
> *Then I try to run things like:*
> server.test(); // Expecting 'Server-level test'
> server.ctrl.test(); // Expecting 'Controller-level test, shadows 
> Server-level');
>
> But I'm running into tons of errors.
>

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/70cbd37d-cbc3-4cb7-8647-0c69fdc68d96%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to