Use an IIFE to Create a Module - Failing

My solution is identical to the example given, but the F12 console gives me this error:


SyntaxError: unknown: Unexpected token (5:25)
  3 |   return 
  4 |   {
> 5 |    isCuteMixin: function (obj) 
    |                          ^
  6 |    {
  7 |       obj.isCute = function () 
  8 |       {

let funModule = ( function () 
{
  return 
  {
   isCuteMixin: function (obj) 
   {
      obj.isCute = function () 
      {
        return true;
      };
    }, 
   singMixin: function (obj) 
   {
      obj.sing = function () 
      {
        console.log("Singing to an awesome tune");
      };
    }
  }
}) ();

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/object-oriented-programming/use-an-iife-to-create-a-module

Not exactly the same ^^
Check this: MDN - Return statement #autoinserted semicolon

You only think it’s the same because of the way you formatted your curly braces — on a newline. If you put them on the same line, this would become obvious

let funModule = ( function () 
{ // you are not returning anything here
  return 
  {
   isCuteMixin: function (obj) 
...

let funModule = ( function () 
{ // but here you are returning an object
  return {
   isCuteMixin: function (obj) 
   {

the expression after a return statement has to be on the same line because of automatic semi-colon insertion

The return statement is affected by automatic semicolon insertion (ASI). No line terminator is allowed between the return keyword and the expression.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return

1 Like

Yeah, forgot to update after reading the link given by @Layer . I figured it out. Can’t say that I like it, but I understand what’s going on.

I’m a relatively noobish developer, but I’ve been battling auto-English constructs since Microsoft Word first decided it was better at grammar than I am. It’s easy to imagine that I’ll hate auto-code just as much. At least with Word I can turn all that junk off.

Anyway, thanks for the help!

1 Like