Javascript comments on same line as code: yes or n

2019-09-22 09:06发布

问题:

So here's some code I've written in Javascript:

var panRate = 120; //Max speed of the camera
var panAccelerate = 0.01; //Amount speed increases each frame
var panDecelerate = 1.05; //Amount speed is divided by each frame

Is that kosher, putting the comments on the same line as the code? Firefox and Chrome don't seem to be having any problems with it, but does that have the potential to break certain browsers?

回答1:

Javascript has 2 types of comments, and both work in all browsers.

  • // comment denotes an "inline" comment. Any characters on that line after the // are a comment.
  • /* comment */ are "block" comments. Any characters withing the /* and */ are a comment, and can span multiple lines.

Both are valid in all javascript implementations, universally.

Style is the only real concern here. And that is a far more subjective question.



回答2:

... but does that have the potential to break certain browsers?

No, not any browser that's remotely compliant with the ECMAScript specification. No browser I've ever seen has any problem with them. This is incredibly basic, if a browser has an issue with this, it will have bigger issues for you to worry about. So don't worry about this.



回答3:

Your fine, the commented out code begins after the backslashes and ends at the end of the line.