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.

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
  • 323,592
  • 20
  • 270
  • 324