-3

Below is my example, i create a .js file let other .js file can use it as a Helper.

var Helper = {
    print: function() {
        console.log("test");
    },

    show: function() {
        print();
    }
}

module.exports = Helper;

In other .js file I can include Helper file like below

 var Helper = require('./Helper.js');
 Helper.print(); // i will get "test" on console.

However, if I do like blow, it cannot find print function in same Helper file.

 var Helper = require('./Helper.js');
 Helper.show();

What can I do to use functions which from same Helper js file?

Dreams
  • 8,288
  • 10
  • 45
  • 71

1 Answers1

0

This doesn't really have anything to do with modules or files.

The function is stored on a property of an object. It isn't an in-scope variable.

You need to either:

  1. Refer to the object (this.print()) (see also How does the this keyword work?)
  2. Make it an in-scope variable

Such:

function print() {
    console.log("test");
};

var Helper = {
    print: print
    show: function() {
        print();
    }
}

module.exports = Helper;
Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335