0

Consider the following simple script:

var sourcePath = 'src';

function setPath(path){
    sourcePath = path || sourcePath;
    return sourcePath;
}

console.log('sourcePath is ' + setPath('somepath') + ' from setPath.js calling the function');
console.log('sourcePath is ' + sourcePath + ' from setPath.js sourcePath vaue ');

exports.getCustomContextPath = setPath;

exports.sourcePath = sourcePath;

The output of running this script is as expected, when it is run:

sourcePath is somepath from setPath.js calling the function
sourcePath is somepath from setPath.js sourcePath value

If I import the same file in a tester file and use it like so:

const pathSetter = require('./setPath');

var test = pathSetter.getCustomContextPath('./temp/test');

console.log('the path set from test.js ' + test + " by calling getCustomContextPath function with '.temp/test'" )
console.log('the path set from test.js ' + pathSetter.sourcePath + " this is the sourcePath value after calling with '.temp/tr'" )

var test2 = pathSetter.getCustomContextPath();
console.log('the path set from test.js' + test2 + " by calling getCustomContextPath function with no parameter " )

console.log('the path set from test.js ' + pathSetter.sourcePath + " this is the sourcePath value after calling with no parameter")

I expect the value of sourcePath variable to be changed when setPath function is called but as you see below, when printing out the value of sourcePath, it is still set to its default value somePath. Here is the output of running the test script:

sourcePath is somepath from setPath.js calling the function
sourcePath is somepath from setPath.js sourcePath value
the path set from test.js ./temp/test by calling getCustomContextPath function with '.temp/test'
the path set from test.js somepath this is the sourcePath value after calling with '.temp/tr'
the path set from test.js./temp/test by calling getCustomContextPath function with no parameters
the path set from test.js somepath this is the sourcePath value after calling with no parameters 

Can anyone tell me why the value of sourcePath is not changed after I expected the script function to change it, but only when I directory access it from the test file as oppose to when the function call returns it?

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
Sean12
  • 667
  • 1
  • 5
  • 14
  • your console.log is hard to read. I think it would be better if you would write more like `console.log('function call with parameter', test1)` – Roman Aug 24 '17 at 16:30

2 Answers2

0

When you are doing exports.sourcePath = sourcePath; you are making an simple assignation.

var sourcePath is not a pointer. So it get initialized at export time as somepath and never change.

If you want to retrieve the good sourcePath value, you can do as exports.sourcePath = () => sourcePath;

Orelsanpls
  • 22,456
  • 6
  • 42
  • 69
  • 1
    Thank you for your response and I understand what you are saying. However, I did expect exports.sourcePath to be a pointer, if some code changes the value of this variable, exports.sourcePath should now (I expected) point to the new value, as it is when the function inside of the script itself changes the value and then it is reflected in the console.log(sourcePath). – Sean12 Aug 24 '17 at 16:47
  • I understand your struggling. [here](https://stackoverflow.com/questions/10231868/pointers-in-javascript) is a great post explaining how _pointers_ works in javascript – Orelsanpls Aug 24 '17 at 16:54
0
exports.sourcePath = sourcePath;

Here sourcePath is not set by reference. Once you exported a value in exports.sourcePath, it will not change even if you change the value of global variable "sourcePath";

consider it like you have written

exports.sourcePath = "somepath";

Now how it is going to change, there is no way to change it now.

Your confusion is that, you set it once it the same file, but in that case, before executing exports.sourcePath, you changed its values to "somePath".

Now what you can try is, try to remove these two lines from your first file

console.log('sourcePath is ' + setPath('somepath') + ' from setPath.js calling the function');
console.log('sourcePath is ' + sourcePath + ' from setPath.js sourcePath vaue ');

and then try to print its value in different file. It will always print "src".

ManishKumar
  • 1,476
  • 2
  • 14
  • 22