2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: bd7123c9c441eddfaeb4bdef
|
|
|
|
title: Comment Your JavaScript Code
|
|
|
|
challengeType: 1
|
2020-04-29 18:29:13 +08:00
|
|
|
videoUrl: 'https://scrimba.com/c/c7ynnTp'
|
|
|
|
forumTopicId: 16783
|
|
|
|
localeTitle: 给代码添加注释
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
2020-04-29 18:29:13 +08:00
|
|
|
<section id='description'>
|
|
|
|
被注释的代码块在 JavaScript 之中是不会执行的。在代码中写注释是一个非常好的方式让你自己和其他人理解代码。
|
|
|
|
JavaScript 中的注释方式有以下两种:
|
|
|
|
使用<code>//</code>注释掉当前行的代码
|
|
|
|
|
|
|
|
```js
|
|
|
|
// This is an in-line comment.
|
|
|
|
```
|
|
|
|
|
|
|
|
你也可以使用多行注释来注释你的代码,以<code>/*</code>开始,用<code>*/</code>来结束,就像下面这样:
|
|
|
|
|
|
|
|
```js
|
|
|
|
/* This is a
|
|
|
|
multi-line comment */
|
|
|
|
```
|
|
|
|
|
|
|
|
<strong>最佳实践</strong><br>写代码的时候,要定期添加注释对部分代码块进行解释。适当的注释能让别人和你自己更容易看懂代码。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Instructions
|
2020-04-29 18:29:13 +08:00
|
|
|
<section id='instructions'>
|
|
|
|
尝试创建这两种类型的注释。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
|
|
|
tests:
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: 创建一个<code>//</code>样式的注释, 被注释的文本至少要包含 5 个字符。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(code.match(/(\/\/)...../g));
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: 创建一个<code>/* */</code>样式的注释, 被注释的文本至少要包含 5 个字符。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(code.match(/(\/\*)([^\/]{5,})(?=\*\/)/gm));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
2020-04-29 18:29:13 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
```js
|
2020-04-29 18:29:13 +08:00
|
|
|
// Fake Comment
|
|
|
|
/* Another Comment */
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-04-29 18:29:13 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
</section>
|