0

I am creating a simple animal.js module which extend in color.js module and output in app.js.

Animal.js:

var exports = module.exports = {};

exports.animalName = function() {
        console.log('Animal Name: Dog');
}

Color.js: Here I am extending the module animal.js and using it as a method of Color module i:e pAnimal();

var animal = require('./animal.js');

exports.animalColor = function() {
        console.log('Color is Black');

        function pAnimal() {
                var pAnimal = animal;
                pAnimal.animalName;
        }
}

App.js: Here I am trying to get value from color module as //Animal Name: Dog & //Color is Black

var localAnimal = require('./color.js');

localAnimal.animalColor();
localAnimal.animalColor.pAnimal();

But when I run this in node server I get error like this:

    D:\node\module-extend>node app.js
    Color is Black
    D:\node\module-extend\app.js:4
    localAnimal.animalColor.pAnimal();
                        ^

TypeError: localAnimal.animalColor.pAnimal is not a function
    at Object.<anonymous> (D:\node\module-extend\app.js:4:25)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3

Please help. Thanks

Sam
  • 481
  • 3
  • 9
  • 22

3 Answers3

0

Here are some answers about nested functions: JavaScript Nested function

pAnimal is a function that can be called only inside animalColor scope.

To access it outside you have to do this:

exports.animalColor = function() {
        console.log('Color is Black');

        this.pAnimal = function() {
                var pAnimal = animal;
                pAnimal.animalName();
        }
}

Then:

var localAnimal = new require('./color.js');
localAnimal.pAnimal();
Jorge Altieri
  • 134
  • 1
  • 6
  • Tried your code and here's the output: D:\node\module-extend>node app.js Color is Black D:\node\module-extend\app.js:4 localAnimal.animalColor.pAnimal(); ^ TypeError: localAnimal.animalColor.pAnimal is not a function – Sam Mar 08 '18 at 07:24
  • Call it as localAnimal.pAnimal(); – Jorge Altieri Mar 08 '18 at 07:33
  • outputs only 'Color is Black' – Sam Mar 08 '18 at 08:03
  • The () were missing in pAnimal.animalName(). Fixed. – Jorge Altieri Mar 08 '18 at 08:33
  • This works!!. Could you please give some explanation why 'new' keyword is used and why pAnimal() is called directly on localAnimal variable ..for learning and future reference also. Thanks. – Sam Mar 08 '18 at 08:40
  • It’s the constructor pattern. AnimalColor works as a constructor and using “new” you’re creating a new instance. This is a great article to learn different ways to do it: https://team.goodeggs.com/export-this-interface-design-patterns-for-node-js-modules-b48a3b1f8f40 – Jorge Altieri Mar 08 '18 at 16:16
0

Return the function in Color.js

exports.animalColor = function() {
        console.log('Color is Black');

       return  function pAnimal() {
                var pAnimal = animal;
                pAnimal.animalName();
        }
}

Call this way in app.js. just call below function it will print Color is Black Animal Name: Dog

localAnimal.animalColor()();
Anusha kurra
  • 482
  • 3
  • 9
  • I tried your code and this give the output as: Color is Black Color is Black – Sam Mar 08 '18 at 07:18
  • this way your trying to just get a output. But does not solve the purpose. My problem is with the error : localAnimal.animalColor.pAnimal is not a function. Now in projects you have to call function inside a function by there namebut here i am calling it without name. – Sam Mar 08 '18 at 07:30
0

You need to assign the function to a property of the outer function to invoke it
Like

function initValidation()
    {
        // irrelevant code here
        function validate(_block){
            console.log( "test", _block );
        }
    
        initValidation.validate = validate;
    }

    initValidation();
    initValidation.validate( "hello" );

So try

exports.animalColor = function() {
        console.log('Color is Black');
        animalColor.pAnimal=pAnimal;
        function pAnimal() {
                var pAnimal = animal;
                pAnimal.animalName;
        }
}
manish kumar
  • 4,412
  • 4
  • 34
  • 51
  • I copied your code and here's the output : animalColor.pAnimal=pAnimal; ^ ReferenceError: animalColor is not defined – Sam Mar 08 '18 at 07:17
  • so pass the animal color to the function so that it traverses to the inner function. atleast the **localAnimal.animalColor.pAnimal is not a function** error is gone – manish kumar Mar 08 '18 at 08:30