Reputation: 1499
I was going through node.js
where i came across something like this var exports = module.exports = {};
in the modules section .
This would be much similar to var a=b.c={}
, how do i visualize it?
What's happening here?
From what i can understand is that both a
and b
are assigned as object, they both describe the same thing or is it that they both references same thing?
Example
exports.sayHelloInEnglish = function() {
return "HELLO";
};
exports.sayHelloInSpanish = function() {
return "Hola";
};
Same as
module.exports = {
sayHelloInEnglish: function() {
return "HELLO";
},
sayHelloInSpanish: function() {
return "Hola";
}
};
Can someone please explain it to me in much simpler terms?
Upvotes: 0
Views: 189
Reputation: 3752
According to the parse tree of the statement, here are the things that happen in order.
For example, you can try this.
var a = [1,5]
var b = a
b[1] = 2
console.log(a)
console.log(b)
Both will have updated values.
Upvotes: 0
Reputation: 665020
var exports = module.exports = {};
, how do i visualize it?
First make sure to understand how it is parsed, what components the syntax consists of. It's basically two nested assignments1:
var exports = (module.exports = {});
You can translate this into the equivalent
var _temp = {};
module.exports = _temp;
var exports = _temp;
So yes, they both will contain the same object.
1: Grammatically it's an assignment expression in the initialiser of a variable declaration, but for ease of understanding we ignore the var
Upvotes: 3