0

I have just started nodejs and following a video tutorial series on Lynda.com and after finishing few sessions, I have the following question :

When I write

var softly = function softly(message){
console.log('proclaiming : '+message);
};

var loudly = function loudly(message){
    console.log('PROCLAIMING : '+message);
};

exports.softly = softly;
exports.loudly = loudly;

How the mapping of property softly exports.softly = softly; is happening, as there is a variable as well as a function with name softly?

Naveen
  • 7,944
  • 12
  • 78
  • 165
  • No, there are not two variables with the same name. `softly` points to a function named `softly`. If you change the reference, say `softly = 'x'` then you'll loose access to the function too. – Sergiu Paraschiv Aug 25 '15 at 13:16
  • possible duplicate of [Named function expressions with matching variable name](http://stackoverflow.com/questions/8333428/named-function-expressions-with-matching-variable-name) – Ben Fortune Aug 25 '15 at 13:17
  • you are adding a property to `exports` object. It could have been `exports.sth = softly` but for name consistency you are doing this. – Sami Aug 25 '15 at 13:17
  • 1
    possible duplicate of [Function and variable with the same name](http://stackoverflow.com/questions/15057649/function-and-variable-with-the-same-name) – Sergiu Paraschiv Aug 25 '15 at 13:17
  • So basically you need to read about function and variable hoisting. – Sergiu Paraschiv Aug 25 '15 at 13:18

1 Answers1

0
var softly = function softly(message){
  console.log('proclaiming : '+message);
};

is equivalent to:

var softly = function (message){ 
  console.log('proclaiming : '+message);
};

which is also equivalent to

function softly(message){
   console.log('proclaiming : '+message);
};

so yes there is function with the name softly created (a function is a variable in javascript ) which is assigned to the same variable. The declaration seems to be redundant, I dont' know there write it like this.

tomsoft
  • 4,448
  • 5
  • 28
  • 35