3

I don't know whats wrong with this code

  var createWorker = function(){
  var task1 = function(){
     console.log("this is job1");
  };
  var task2 = function(){
     console.log("this is job2");
  };

   return
   {
      job1: task1,
      job2: task2
   };
};
var worker = createWorker();
worker.job1();
worker.job2();

This gives the syntax error but I think the syntax is right. Can anyone help? Thank you.

Ali
  • 189
  • 1
  • 13

1 Answers1

8

You are returning undefined, because of the automatic semicolon insertation (ASI).

return                            // colon is inserted here
     {                            // never reached
         job1: task1,
         job2: task2
     };

You could move the bracket into the line of the return statement.

return {
         job1: task1,
         job2: task2
     };
Nina Scholz
  • 351,820
  • 24
  • 303
  • 358
  • Thanks @Nina Scholz. Finally It works. I did not know about such minor details. (Y) – Ali Jul 23 '17 at 08:04