aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/statements/import.meta/index.html
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/statements/import.meta/index.html
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/statements/import.meta/index.html')
-rw-r--r--files/zh-cn/web/javascript/reference/statements/import.meta/index.html104
1 files changed, 104 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/statements/import.meta/index.html b/files/zh-cn/web/javascript/reference/statements/import.meta/index.html
new file mode 100644
index 0000000000..95f5f68e36
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/statements/import.meta/index.html
@@ -0,0 +1,104 @@
+---
+title: import.meta
+slug: Web/JavaScript/Reference/Statements/import.meta
+tags:
+ - JavaScript
+ - Language feature
+ - Modules
+ - Reference
+ - Statement
+ - import
+translation_of: Web/JavaScript/Reference/Statements/import.meta
+---
+<div>{{JSSidebar("Statements")}}</div>
+
+<p><code>import.meta</code>是一个给JavaScript模块暴露特定上下文的元数据属性的对象。它包含了这个模块的信息,比如说这个模块的URL。</p>
+
+<h2 id="语法">语法</h2>
+
+<pre class="syntaxbox">import.meta</pre>
+
+<h2 id="描述">描述</h2>
+
+<p><code>import.meta</code>对象由一个关键字<code>"import"</code>,一个点符号和一个<code>meta</code>属性名组成。通常情况下<code>"import."</code>是作为一个属性访问的上下文,但是在这里<code>"import"</code>不是一个真正的对象。</p>
+
+<p><code>import.meta</code>对象是由ECMAScript实现的,它带有一个{{jsxref("null")}}的原型对象。这个对象可以扩展,并且它的属性都是可写,可配置和可枚举的。</p>
+
+<h2 id="示例">示例</h2>
+
+<p>这里有一个 <code>my-module.mjs模块</code></p>
+
+<pre class="brush: html">&lt;script type="module" src="my-module.mjs"&gt;&lt;/script&gt;
+</pre>
+
+<p>你可以通过 <code>import.meta</code> 对象获取这个模块的元数据信息.</p>
+
+<pre class="brush: js">console.log(import.meta); // { url: "file:///home/user/my-module.mjs" }</pre>
+
+<p>它返回一个带有<code>url</code>属性的对象,指明模块的基本URL。也可以是外部脚本的URL,还可以是内联脚本所属文档的URL。</p>
+
+<p>注意,url也可能包含参数或者哈希(比如后缀<code>?</code>或<code>#</code>)</p>
+
+<p>以下面的HTML为例:</p>
+
+<pre>&lt;script type="module"&gt;
+import './index.mjs?someURLInfo=5';
+&lt;/script&gt;</pre>
+
+<p>...下面的JavaScript会打印<code>someURLInfo</code>这个参数:</p>
+
+<pre>// index.mjs
+new URL(import.meta.url).searchParams.get('someURLInfo'); // 5</pre>
+
+<p>在脚本中引入别的脚本同样生效:</p>
+
+<pre>// index.mjs
+import './index2.mjs?someURLInfo=5';
+
+// index2.mjs
+new URL(import.meta.url).searchParams.get('someURLInfo'); // 5</pre>
+
+<p>虽然在上述例子中,Node.js允许携带参数(或哈希),但以Node 14.1.0为例,使用<code>node --experimental-modules index.mjs?someURLInfo=5</code> 执行脚本,查询URL参数将会报错(该环境下<code>index.mjs?someURLInfo=5</code> 被视作一个文件而不是URL)</p>
+
+<p>像这种特定于文件的参数传递可能是对应用范围内<code>location.href</code>(ps: 通过在HTML路径添加参数或哈希)(而在Node.js中是<code>process.env</code>)的补充</p>
+
+<h2 id="规范">规范</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">规范</th>
+ <th scope="col">状态</th>
+ <th scope="col">备注</th>
+ </tr>
+ <tr>
+ <td><a href="https://github.com/tc39/proposal-import-meta">tc39/proposal-import-meta</a></td>
+ <td>Stage 3</td>
+ <td>Initial definition.</td>
+ </tr>
+ <tr>
+ <td><a href="https://html.spec.whatwg.org/multipage/webappapis.html#hostgetimportmetaproperties">HTML Standard</a></td>
+ <td>Living Standard</td>
+ <td>Defines import.meta properties in HTML.</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器支持">浏览器支持</h2>
+
+
+
+<p>{{Compat("javascript.statements.import_meta")}}</p>
+
+<h3 id="Implementation_Progress">Implementation Progress</h3>
+
+<p>The following table provides a daily implementation status for this feature, because this feature has not yet reached cross-browser stability. The data is generated by running the relevant feature tests in <a href="https://github.com/tc39/test262">Test262</a>, the standard test suite of JavaScript, in the nightly build, or latest release of each browser's JavaScript engine.</p>
+
+<p>{{EmbedTest262ReportResultsTable("import.meta")}}</p>
+
+<h2 id="See_also">See also</h2>
+
+<ul>
+ <li><a href="/en-US/docs/Web/JavaScript/Reference/Statements/import">import</a></li>
+ <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/export">export</a></code></li>
+</ul>