Javascript Error When Minified
Solution 1:
You shouldn't be getting an "unexpected identifier" error from the minified code you've presented. If you are, it's a bug in the JavaScript engine you're using it with. That was true of the code you posted originally, which was:
function arc(r,p,a){return"A"+r+","+r+" 0 "+ +(a>Math.PI)+",1 "+p;}
// You used to have a space here -----------^
But with the updated code you've posted:
function arc(r,p,a){return"A"+r+","+r+" 0 "++(a>Math.PI)+",1 "+p;}
// No space here anymore -------------------^
...it's a problem, because the ++
is an increment operator (either the prefix [++a
] or postfix [a++
]). And that would need an identifier (the thing to increment). The ++
just isn't valid in that position (the exact error you get is likely to vary by JavaScript engine).
You can defend the code against the minifier doing this by changing it slightly:
function arc(r, p, a) {
return "A" + r + "," + r + " 0 " + (+(a > Math.PI)) + ",1 " + p;
}
The parens prevent the +
and the other +
from getting combined into ++
. They also make the intent a bit clearer, IMHO.
Re the second part of your question, no, you can't remove that +
, it will change how the function works. In particular, a > Math.PI
will be true
or false
, and the +
is there to make it a number (1
for true
, 0
for false
) before it's concatenated to the string. If you remove it, you'll see true
or false
in the string instead of 1
or 0
.
Solution 2:
I guess the problem isn't really there, but just before, even if it looks like it's here because the invalid token is function
. Try to add ;
:
;function arc(r,p,a){return"A"+r+","+r+" 0 "+ +(a>Math.PI)+",1 "+p;};
I'm a little surprised that the minifier did let the ;
before the }
, though. It's useless.
Post a Comment for "Javascript Error When Minified"