1
0
mirror of https://github.com/ruanyf/es6tutorial.git synced 2025-05-24 02:02:21 +00:00

docs(operator): #! 命令

This commit is contained in:
ruanyf 2023-04-08 00:17:12 +08:00
parent b68c36d912
commit 50e9ced0da
2 changed files with 42 additions and 42 deletions

View File

@ -307,3 +307,45 @@ function example(opts) {
}
```
## `#!`命令
Unix 的命令行脚本都支持`#!`命令,又称为 Shebang 或 Hashbang。这个命令放在脚本的第一行用来指定脚本的执行器。
比如 Bash 脚本的第一行。
```bash
#!/bin/sh
```
Python 脚本的第一行。
```python
#!/usr/bin/env python
```
[ES2023](https://github.com/tc39/proposal-hashbang) 为 JavaScript 脚本引入了`#!`命令,写在脚本文件或者模块文件的第一行。
```javascript
// 写在脚本文件第一行
#!/usr/bin/env node
'use strict';
console.log(1);
// 写在模块文件第一行
#!/usr/bin/env node
export {};
console.log(1);
```
有了这一行以后Unix 命令行就可以直接执行脚本。
```bash
# 以前执行脚本的方式
$ node hello.js
# hashbang 的方式
$ ./hello.js
```
对于 JavaScript 引擎来说,会把`#!`理解成注释,忽略掉这一行。

View File

@ -482,48 +482,6 @@ class FakeWindow extends Realm {
上面代码中,`FakeWindow`模拟了一个假的顶层对象`window`
## `#!`命令
Unix 的命令行脚本都支持`#!`命令,又称为 Shebang 或 Hashbang。这个命令放在脚本的第一行用来指定脚本的执行器。
比如 Bash 脚本的第一行。
```bash
#!/bin/sh
```
Python 脚本的第一行。
```python
#!/usr/bin/env python
```
现在有一个[提案](https://github.com/tc39/proposal-hashbang),为 JavaScript 脚本引入了`#!`命令,写在脚本文件或者模块文件的第一行。
```javascript
// 写在脚本文件第一行
#!/usr/bin/env node
'use strict';
console.log(1);
// 写在模块文件第一行
#!/usr/bin/env node
export {};
console.log(1);
```
有了这一行以后Unix 命令行就可以直接执行脚本。
```bash
# 以前执行脚本的方式
$ node hello.js
# hashbang 的方式
$ ./hello.js
```
对于 JavaScript 引擎来说,会把`#!`理解成注释,忽略掉这一行。
## JSON 模块
import 命令目前只能用于加载 ES 模块,现在有一个[提案](https://github.com/tc39/proposal-json-modules),允许加载 JSON 模块。