Javascript/Jquery commenting causing errors in IE

2020-02-07 03:58发布

问题:

So this post is not so much of a "please help me fix it" post as much as it is a "why would changing that make it work?". So I had some javascript/jquery written that was working in firefox and chrome, but IE threw errors.

I could not figure it out even with the helpful posts from users here at stackoverflow. I eventually stumbled upon the answer (as I seem to find myself doing a lot with coding).

I was doing a somewhat rigorous style of commenting taught to me by one of my computer science professors where a function would have commenting such as this:

//@ describe function
//@ params: param1 - function, param2 - function
//@ etc....

So I foolishly threw this into my javascript only to find out that IE really did not care for this much at all. When I removed the @ symbols the code worked perfectly.

So my question is why this caused errors in IE? Shouldn't whatever follows the '//' comments not matter?

回答1:

You might be thinking of docblock commenting, which you would want to wrap in block comment syntax:

/**
 * Function does this
 * @param <string> $str The string
 * @param <array> $arr The array
 * @return <bool> true if string is in array, false if not
*/

I can see IE just being stupid. Odds are even if there is an explanation for why your //@ didn't work, it'll likely be a really stupid one, and odds are that only a small percentage of us would even be able to recreate it on our version of IE.

IE is the only browser to my knowledge that looks at conditional comments, so I can see them having a different comment parser than all other browsers which would conflict with otherwise proper code.



回答2:

It does indeed look like some kooky IE conditional comment support. It appears that if @ is the first character of a comment (whether it starts with //@ or /*@, then IE looks for a conditional comment directive after the @ sign. See http://msdn.microsoft.com/en-us/library/8ka90k2e(v=vs.94).aspx for some examples.

AlienWebguy's suggestion should work because the first character of the comment is *. You could probably also just put a space before the @ sign:

// @ describe function
// @ params: param1 - function, param2 - function
// @ etc....