0

I am trying to make a web application. I have a task controller in a separate .js file which I include in the main .html file:

<head>
 <script src = "tasks-controller.js"></script>
</head>

and the file tasks-controller.js:

tasksController = function() {
 var taskPage;
 var initialized = false;
 return
 {
 init: function(page) {
 if(!initialized)
 {
 .... some manipulation with jQuery... 
 initialized = true; 
 }
 }
 }
} ();

Back in my main .html file I call tasks controller right after body closing element:

<script>
$(document).ready(function()
{
 tasksController.init($('#taskPage'));
}
</script>

When I test the file, in Google chrome tools, I get Uncaught SyntaxError: Unexpected token ( on line 8 of tasks-controller.js which is this line:

init: function(page)

along with another error that 'tasksController' is not defined when I call it above. Any suggestions to what I am doing wrong? Thanks!

asked Jun 26, 2015 at 5:22
2
  • couldn't see any problem in your code. Commented Jun 26, 2015 at 5:26
  • Hmm, I don't see anything wrong in particular, unless there are magic spaces. Commented Jun 26, 2015 at 5:26

2 Answers 2

1

The ready method is where the error is. You missed the ) of ready.

See the highlighted code and comments in the code below.

$(document).ready(function() {
 tasksController.init($('#taskPage'));
}); // The ) you've missed
// ^^^

Other problem in your code is the return statement. If you put the object to the next line of return it will always return undefined. Automatic semicolon insertion will cause the code to put semicolon after return.

So, it'll be like return;.

Use following code:

tasksController = function() {
 var taskPage;
 var initialized = false;
 return {
 init: function(page) {
 if (!initialized) {
 ....some manipulation with jQuery...
 initialized = true;
 }
 }
 };
}();
answered Jun 26, 2015 at 5:28
Sign up to request clarification or add additional context in comments.

5 Comments

Um, there is one there.
@Scimonster <script> $(document).ready(function() { tasksController.init($('#taskPage')); } </script> Don't see in this
DownVoters, please add comments here
I think automatic semicolon insertions happens when minified. I may be wrong.
@BhojendraNepal NO! It happens for all the javascripts
0

Due to implicit semicolons, your code equals this:

return;
{
 init: function(page) {
 if(!initialized)
 {
 .... some manipulation with jQuery... 
 initialized = true; 
 }
 }
}

The return is returning nothing, and so the {} makes a new block. Now you can see why you have a syntax error.

Just change the first line to return { (no newline) and you should be good.

See Strangest language feature.

answered Jun 26, 2015 at 5:30

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.