I am trying to run this short program from Eloquent Javascript in the "Modules" section.
var weekDay = function() {}();
(function(exports) {
var names = ["Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"];
exports.name = function(number) {
return names[number];
};
exports.number = function(name) {
return names.indexOf(name);
};
})(this.weekDay = {});
console.log(weekDay.name(weekDay.number("Saturday")));
The correct conclusion should be // -> Saturday.
It works fine in the browser. However, when I try to run it in the Node interpreter, I get this error:
TypeError: Cannot read property 'name' of undefined
I can only assume that it has something to do with how Node handles the export keyword. Can someone help me get at least a rough understanding of this behavior?
source
share