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