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

JavaScript

Sub 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
Steven Parker
243,656 Points

It looks like you are adding this module as a property named sub to an existing object named awesomeNewModule.

:point_right: 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.

Steven Parker
Steven Parker
243,656 Points

Also, I noticed he has "bar" where you have "bad", but I can't imagine that makes a difference.

Here's the link of the video.

Snippet is at around 13:45

I thought I posted this on the video discussion, sorry

Steven Parker
Steven Parker
243,656 Points

Interesting, I notice he never saves or executes the code after he adds ".sub". I wonder if he would have gotten a syntax error also.

:point_right: Try it without the "var".

Without 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
Steven Parker
243,656 Points

Well, 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."