From 18520b88e69cdcb23006b14e9698dcc80c35990a Mon Sep 17 00:00:00 2001 From: ruanyf Date: Sat, 28 Jun 2025 20:23:46 +0800 Subject: [PATCH] docs(module): edit import attributes --- docs/module.md | 9 ++++++++- docs/proposals.md | 44 -------------------------------------------- 2 files changed, 8 insertions(+), 45 deletions(-) diff --git a/docs/module.md b/docs/module.md index 5e8d280..a5b1824 100644 --- a/docs/module.md +++ b/docs/module.md @@ -576,6 +576,13 @@ const configData = await import( 上面代码中,import 命令使用 with 子句,指定一个属性对象。这个属性对象目前只有一个 type 属性,它的值就是导入代码的类型,现在只能设置为`json`一个值。 +如果没有 import 属性,导入 JSON 数据只能使用 fetch 命令。 + +```javascript +const response = await fetch('./config.json'); +const json = await response.json(); +``` + export 命令与 import 命令写在一起,形成一个再导出语句时,也可以使用 import 属性。 ```javascript @@ -584,7 +591,7 @@ export { default as config } from './config-data.json' with { type: 'json' }; ## 模块的继承 -模块之间也可以继承。 +模块可以继承。 假设有一个`circleplus`模块,继承了`circle`模块。 diff --git a/docs/proposals.md b/docs/proposals.md index 2626982..f6178b5 100644 --- a/docs/proposals.md +++ b/docs/proposals.md @@ -494,47 +494,3 @@ class FakeWindow extends Realm { 上面代码中,`FakeWindow`模拟了一个假的顶层对象`window`。 -## JSON 模块 - -import 命令目前只能用于加载 ES 模块,现在有一个[提案](https://github.com/tc39/proposal-json-modules),允许加载 JSON 模块。 - -假定有一个 JSON 模块文件`config.json`。 - -```javascript -{ - "appName": "My App" -} -``` - -目前,只能使用`fetch()`加载 JSON 模块。 - -```javascript -const response = await fetch('./config.json'); -const json = await response.json(); -``` - -import 命令能够直接加载 JSON 模块以后,就可以像下面这样写。 - -```javascript -import configData from './config.json' assert { type: "json" }; -console.log(configData.appName); -``` - -上面示例中,整个 JSON 对象被导入为`configData`对象,然后就可以从该对象获取 JSON 数据。 - -`import`命令导入 JSON 模块时,命令结尾的`assert {type: "json"}`不可缺少。这叫做导入断言,用来告诉 JavaScript 引擎,现在加载的是 JSON 模块。你可能会问,为什么不通过`.json`后缀名判断呢?因为浏览器的传统是不通过后缀名判断文件类型,标准委员会希望遵循这种做法,这样也可以避免一些安全问题。 - -导入断言是 JavaScript 导入其他格式模块的标准写法,JSON 模块将是第一个使用这种语法导入的模块。以后,还会支持导入 CSS 模块、HTML 模块等等。 - -动态加载模块的`import()`函数也支持加载 JSON 模块。 - -```javascript -import('./config.json', { assert: { type: 'json' } }) -``` - -脚本加载 JSON 模块以后,还可以再用 export 命令输出。这时,可以将 export 和 import 结合成一个语句。 - -```javascript -export { config } from './config.json' assert { type: 'json' }; -``` -