Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Christos Peramatzis
16,428 PointsSub Module Pattern - Syntax Error
When I try to run the sub module pattern example, I get a syntax error. My code is exactly the same as on the screen
var awesomeNewModule.sub = (function(exports) {
var exports = {
foo: 5,
bad: 10
};
exports.helloMars = function() {
console.log("Hello Mars!");
};
exports.goodbye = function() {
console.log("Goodbye!");
};
return exports;
}(awesomeNewModule.sub || {} ));
The other examples run perfectly. Why does this happen?
3 Answers
Steven Parker
243,656 PointsIt looks like you are adding this module as a property named sub to an existing object named awesomeNewModule.
If that's the case, perhaps you don't want that "var" keyword at the front?
If that's not it, please share the rest of the code and provide a link to the video. Remember you can snapshot a workspace and provide the link to share all the code at once.
Christos Peramatzis
16,428 PointsHere's the link of the video.
Snippet is at around 13:45
I thought I posted this on the video discussion, sorry
Steven Parker
243,656 PointsInteresting, I notice he never saves or executes the code after he adds ".sub". I wonder if he would have gotten a syntax error also.
Try it without the "var".
Christos Peramatzis
16,428 PointsWithout the "var, I get this:
ReferenceError: awesomeNewModule is not defined
It's him not saving and executing that made me want to try it out, to see if I need to add the .sub to execute the functions in the console.
Steven Parker
243,656 PointsWell, as I said the first time, it appears to be adding "sub" to an existing object named awesomeNewModule.
So besides removing "var", there would have to be an existing object to add the .sub to. This would be consistent with the comment made at 13:09: "Here we're just going to add the sub object to the main module we created."
Steven Parker
243,656 PointsSteven Parker
243,656 PointsAlso, I noticed he has "bar" where you have "bad", but I can't imagine that makes a difference.