I have a javascript module that looks like this:
var TestModule = function(param) {
this.display = function() {
console.log(`testModule.display called, with parameter: ${param}`);
}
}
module.exports = TestModule;
I also have a program that uses this module, like so:
var TestModule = require('./test-module.js');
var testModule = new TestModule('asdf');
testModule.display();
When I run the program, I get the following output:
testModule.display called, with parameter: asdf
All of this seems to be fine so far, and doing what I want: making the value I send in available to functions within the module.
However, I have also seen the following done in module code:
var TestModule = function(param) {
this.param = param;
this.display = function() {
console.log(`testModule.display called, with parameter: ${this.param}`);
}
}
module.exports = TestModule;
Running this second version gives me the same output as the first.
My question is, what is the purpose of this.param = param
? What does that do for me that my original version does not?