0

I basically tried extending modules with two different modules

1st-

var exports=exports.module={};
exports.tutorial= function(){
    console.log('Raj is king');
};

2nd module

var tutor= require('./Tutorial.js');
exports.NodeTutorial= function(){
    console.log('Node Tutorial');
    function pTutor()
    {
        var putor=tutor;
        putor.tutorial();
    }

};

and finally tried to use these modules with a main.js file

var Addition= require('./NodeTutorial.js');
Addition.NodeTutorial();
Addition.NodeTutorial.pTutor();

However when I tried to execute the code i got the msg Type Error: Addition.NodeTutorial.pTutor is not a function

1 Answers1

1

You can't.

pTutor is locally scoped to the function it is declared within. It is not accessible outside that function.

If you want it to be a property of that function, then you need to make it a property.

var tutor = require('./Tutorial.js');
exports.NodeTutorial = function() {
    console.log('Node Tutorial');
}
exports.NodeTutorial.pTutor = function pTutor() {
    var putor = tutor;
    putor.tutorial();
};

Note that while it is possible to attach properties to functions (jQuery does it so you can call, for example both jQuery(...) and jQuery.ajax(...)) it usually results in a confusing API and is best avoided.

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
  • arent you missing a semicolon at the end of the line export.NodeTutorial or is it just me and my lack of knowledge? And how do I call that method pTutor – Maqsud Mallick Oct 25 '20 at 06:40
  • [No, I'm not](https://stackoverflow.com/questions/2846283/what-are-the-rules-for-javascripts-automatic-semicolon-insertion-asi) and *the way you were trying to do it in the question* – Quentin Oct 25 '20 at 08:14