internet explorer - JavaScript Compile Error -


i noticed strange thing when writing js code. compile error seems happen across multiple browsers (tested: chrome, ie, safari). in no hurry solution not figure out why code won't compile:

    function foobar1()//compiles     {         return {             x: 0,             y: 1         };     }     function foobar2()//compiles     {         return {x: 0, y: 1};     }     function foobar3()//compiles     {         return         {             x: 0         };     }     function foobar4()//does not compile     {         return         {             x: 0,             y: 1//uncaught syntaxerror: unexpected token :          };     } 

this due javascript's automatic semicolon insertion "feature".

i put word "feature" in quotes because it's -- it's frankly bit of disaster; 1 of worst design decisions know of in language.

basically, js language designed have statements ending semi-colon, original language design made allowances coders forgot it, , if sees line break without semi-colon, tries guess whether there should have been 1 there.

unfortunately, there cases results in ambiguity , errors, , return statements worst this.

in last example, { object being returned on line after return statement. classic case kind of bug. js automatically inserts semi-colon after return, , the object intended return ignored.

simple answer: don't this. put object on same line return statement. memorise fact return needs return data on same line (or @ least start on same line) return statement itself.

the closest can broken example , still have work shown first example.

return {     x: 0,     y: 1 }; 

as final thought, might want consider running js code through tool jslint, pick errors (and other potential issues).


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -