By default in a module, this
points to the original exports
object that the module system creates when it sets up your module. When you do:
module.exports = {a: 4}
You replace the existing exports
property on the module
object with a new and separate one. But, this
still points at the original, empty exports
object. So, when you do:
console.log(this);
after creating a new exports
object, this
is still pointing at the original exports
object which you never modified. So, it still shows that object as being empty.
If, after reassigning the exports property with a new object like you did above, you then did this:
console.log(module.exports === this); // false
You will see that this
does not point at the new exports
object you assigned as a property on the module
object.
On the other hand, when you do this:
module.exports.a = 4
You are not replacing the existing exports
object. Instead, you're just adding a property to the existing object, so the object you modified is the original exports
object that this
still points to.