Why is there back slash in this piece of code?

I just came across this line of code in javascript:

Math.round(someVariable*100)

Shouldn't that be simply:

Math.round(someVariable*100)

I checked and both seem to give me the same results. Can you thing of any reason why it was coded with a back slash?


Unless that line is somehow surrounded with /* and */ , that's not valid Javascript :-)

The ECMAScript specification only mentions backslashes in the context of strings (including JSONStrings) and regular expressions. If you use the following HTML file:

<!DOCTYPE html>
<html>
  <body>
    <p id="demo">Click the button.</p>
    <button onclick="myFunction()">Try it</button>
    <script>
      function myFunction() {
        document.getElementById("demo").innerHTML = Math.round(2.57*10);
      }
    </script>
  </body>
</html>

you'll find that most browsers give an error:

Firefox 24esr: SyntaxError: illegal character @ ...
Chrome 31    : Uncaught SyntaxError: Unexpected token ILLEGAL
IE 8         : Invalid character

That's all the ones I have on my primary development box.

In fact, since Math.round() returns a value that you're supposed to use somehow, that statement is useless even if it does work. It may be there's some extra context to this question that you haven't provided.

It may be that your particular implementation of the Javascript interpreter is lax with what it accepts, it's hard to discern without knowing the actual interpreter you're using. But, if that's the case, I'd seriously reconsider using it. There are relatively minor differences between implementations but none so far off the mark the they violate the most basic bits of the language specification.


You would need to give us more information, but the reason why i would see * would be escaping a regex *. Are you sure this isnt a part of some string or regex query scanning something?

And Yes, that would confuse me because any one character would go between Math and round.

This is the only idea i can think of which would have an escaped asteriks (*).

链接地址: http://www.djcxy.com/p/80924.html

上一篇: JavaScript函数评估自己并在同一时间调用自己

下一篇: 为什么在这段代码中有反斜杠?