I am using node.js and express.js based programming. I tried using util.inherits to implement inheritance in JavaScript. I tried to do the following:
//request.js function Request() { this.target = 'old'; console.log('Request Target: ' + this.target); } Request.prototype.target = undefined; Request.prototype.process = function(callback) { if (this.target === 'new') return true; return false; } module.exports = Request; //create.js function Create() { Create.super_.call(this); this.target = 'new'; } util.inherits(Create, Request); Create.prototype.process = function(callback) { if (Create.super_.prototype.process.call(this, callback)) { return callback({ message: "Target is 'new'" }); } else { return callback({ message: "Target is not 'new'" }); } } module.exports = Create; //main.js var create = new (require('./create'))(); create.process(function(msg) { console.log(msg); });
My scenario:
I have Request as a base class and Create as a child class. The request has a target field that initializes the old constructor in Request .
Now I create an object of the Create class, which first calls the Request constructor, and then initializes the target field with new . When I call the function of the Create process, I expect to get the target is 'new' message, but it returns another!
I searched for related topics for this, but all this is what I tried! Can someone explain what happened?
Thanks in advance:)
source share