当前位置:嗨网首页>书籍在线阅读

08-关注注释

  
选择背景色: 黄橙 洋红 淡粉 水蓝 草绿 白色 选择字体: 宋体 黑体 微软雅黑 楷体 选择字体大小: 恢复默认

1.3 关注注释

像很多编程语言一样,JavaScript也有在代码中添加注释的语法。这些注释在执行时会被JavaScript所忽略,所有注释只对开发人员或者和开发人员一起工作的同事有意义。因为它允许使用自然语言解释含义不那么明确的代码。在本书中,会在代码实例中添加注释来解释该段代码。

在JavaScript中,有两种不同的注释:单行注释和多行注释。单行注释由两个连在一起的斜线开始(//),直到本行结束。多行注释由一组斜线和星号开始(/),由另一组星号和斜线作为结束(/),可以包含多行文字。以下的例子可以说明这两种注释的不同:

console.log("echo");        // prints "echo" to the console 
/*
 In the previous line, everything up to the double forward slashes
 is JavaScript code, and must be valid syntax. The double
 forward slashes start a comment, and will be ignored by JavaScript.
 This text is in a block comment, and will also be ignored
 by JavaScript. We've chosen to indent the comments of this block
 for readability, but that's not necessary.
*/
/*Look, Ma, no indentation!*/

在本书中,会时不时看到层叠样式表(CSS),CSS中的注释跟JavaScript的多行注释语法一样(CSS不支持单行注释)。HTML(类似CSS)不支持单行注释,它的多行注释也与JavaScript的不一样。HTML中的注释是被<!- -和- - >这种笨重的符号所包围的:

<head>
    <title>HTML and CSS Example</title>
    <style>
        body: { color: red; }
 /* this is a CSS comment...
 which can span multiple lines. */
    </style>
    <script>
        console.log("echo"); // back in JavaScript...
 /* ...so both inline and block comments
 are supported. */
    </script>
</head>