aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/textdecoder/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-cn/web/api/textdecoder/index.html')
-rw-r--r--files/zh-cn/web/api/textdecoder/index.html109
1 files changed, 109 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/textdecoder/index.html b/files/zh-cn/web/api/textdecoder/index.html
new file mode 100644
index 0000000000..a0e7279ba6
--- /dev/null
+++ b/files/zh-cn/web/api/textdecoder/index.html
@@ -0,0 +1,109 @@
+---
+title: TextDecoder
+slug: Web/API/TextDecoder
+tags:
+ - API
+ - DOM
+ - 参考
+ - 接口
+ - 编码
+translation_of: Web/API/TextDecoder
+---
+<div>{{APIRef("Encoding API")}}</div>
+
+<p><code><strong>TextDecoder</strong></code><font><font> 接口表示一个文本解码器,一个解码器只支持一种特定文本编码,例如 </font></font><code>utf-8</code><font><font>、</font></font><code>iso-8859-2</code><font><font>、</font></font><code>koi8</code><font><font>、</font></font><code>cp1261</code><font><font>,</font></font><code>gbk</code><font><font> 等等。</font></font>解码器将字节流作为输入,并提供代码点流作为输出<font><font>。</font></font></p>
+
+<h2 id="例子"><font><font>例子</font></font></h2>
+
+<h3 id="用类型化数组表示文本"><font><font>用类型化数组表示文本</font></font></h3>
+
+<p><font><font>本示例展示如何解码中文/日语字符</font></font><img alt="" src="https://mdn.mozillademos.org/files/16663/2019-05-21_191907.png" style="height: 32px; width: 41px;"><span class="message-body-wrapper"><span class="message-flex-body"><span class="devtools-monospace message-body"><span class="objectBox objectBox-string"><font><font>,用五个不同的数组类型表示</font></font></span></span></span></span><font><span class="message-body-wrapper"><span class="message-flex-body"><span class="devtools-monospace message-body"><span class="objectBox objectBox-string"><font>:</font></span></span></span></span></font> {{jsxref("Uint8Array")}}, {{jsxref("Int8Array")}}, {{jsxref("Uint16Array")}}, {{jsxref("Int16Array")}}, {{jsxref("Int32Array")}}</p>
+
+<pre class="brush: js; line-numbers language-js notranslate"><code class="language-js"><span class="keyword token">let</span> utf8decoder <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">TextDecoder</span><span class="punctuation token">(</span><span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// default 'utf-8' or 'utf8'</span>
+
+<span class="keyword token">let</span> u8arr <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">Uint8Array</span><span class="punctuation token">(</span><span class="punctuation token">[</span><span class="number token">240</span><span class="punctuation token">,</span> <span class="number token">160</span><span class="punctuation token">,</span> <span class="number token">174</span><span class="punctuation token">,</span> <span class="number token">183</span><span class="punctuation token">]</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
+<span class="keyword token">let</span> i8arr <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">Int8Array</span><span class="punctuation token">(</span><span class="punctuation token">[</span><span class="operator token">-</span><span class="number token">16</span><span class="punctuation token">,</span> <span class="operator token">-</span><span class="number token">96</span><span class="punctuation token">,</span> <span class="operator token">-</span><span class="number token">82</span><span class="punctuation token">,</span> <span class="operator token">-</span><span class="number token">73</span><span class="punctuation token">]</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
+<span class="keyword token">let</span> u16arr <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">Uint16Array</span><span class="punctuation token">(</span><span class="punctuation token">[</span><span class="number token">41200</span><span class="punctuation token">,</span> <span class="number token">47022</span><span class="punctuation token">]</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
+<span class="keyword token">let</span> i16arr <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">Int16Array</span><span class="punctuation token">(</span><span class="punctuation token">[</span><span class="operator token">-</span><span class="number token">24336</span><span class="punctuation token">,</span> <span class="operator token">-</span><span class="number token">18514</span><span class="punctuation token">]</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
+<span class="keyword token">let</span> i32arr <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">Int32Array</span><span class="punctuation token">(</span><span class="punctuation token">[</span><span class="operator token">-</span><span class="number token">1213292304</span><span class="punctuation token">]</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
+
+console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span>utf8decoder<span class="punctuation token">.</span><span class="function token">decode</span><span class="punctuation token">(</span>u8arr<span class="punctuation token">)</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
+console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span>utf8decoder<span class="punctuation token">.</span><span class="function token">decode</span><span class="punctuation token">(</span>i8arr<span class="punctuation token">)</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
+console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span>utf8decoder<span class="punctuation token">.</span><span class="function token">decode</span><span class="punctuation token">(</span>u16arr<span class="punctuation token">)</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
+console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span>utf8decoder<span class="punctuation token">.</span><span class="function token">decode</span><span class="punctuation token">(</span>i16arr<span class="punctuation token">)</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
+console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span>utf8decoder<span class="punctuation token">.</span><span class="function token">decode</span><span class="punctuation token">(</span>i32arr<span class="punctuation token">)</span><span class="punctuation token">)</span><span class="punctuation token">;</span></code></pre>
+
+<h3 id="处理非UTF8文本"><font><font>处理非UTF8文本</font></font></h3>
+
+<p><font><font>在此示例中,我们对俄语文本“Привет,мир!”(</font></font> "Hello, world."<font><font>)进行解码。</font><font>在我们的</font></font> {{domxref("TextDecoder/TextDecoder", "TextDecoder()")}} <font><font>构造函数中,我们指定Windows-1251字符编码,适用于西里尔字母。</font></font></p>
+
+<pre class="brush: js; line-numbers language-js notranslate"><code class="language-js"><span class="keyword token">let</span> win1251decoder <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">TextDecoder</span><span class="punctuation token">(</span><span class="string token">'windows-1251'</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
+<span class="keyword token">let</span> bytes <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">Uint8Array</span><span class="punctuation token">(</span><span class="punctuation token">[</span><span class="number token">207</span><span class="punctuation token">,</span> <span class="number token">240</span><span class="punctuation token">,</span> <span class="number token">232</span><span class="punctuation token">,</span> <span class="number token">226</span><span class="punctuation token">,</span> <span class="number token">229</span><span class="punctuation token">,</span> <span class="number token">242</span><span class="punctuation token">,</span> <span class="number token">44</span><span class="punctuation token">,</span> <span class="number token">32</span><span class="punctuation token">,</span> <span class="number token">236</span><span class="punctuation token">,</span> <span class="number token">232</span><span class="punctuation token">,</span> <span class="number token">240</span><span class="punctuation token">,</span> <span class="number token">33</span><span class="punctuation token">]</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
+console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span>win1251decoder<span class="punctuation token">.</span><span class="function token">decode</span><span class="punctuation token">(</span>bytes<span class="punctuation token">)</span><span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// Привет, мир!</span></code></pre>
+
+<h2 id="构造函数">构造函数</h2>
+
+<dl>
+ <dt>{{DOMxRef("TextDecoder.TextDecoder", "TextDecoder()")}}</dt>
+ <dd><font><font>返回一个新构造的 </font></font><code>TextDecoder</code><font><font>,它使用参数中指定的解码方法生成代码点流。</font></font></dd>
+</dl>
+
+<h2 id="属性">属性</h2>
+
+<p><em><code>TextDecoder</code><font><font> 接口不继承任何属性。</font></font></em></p>
+
+<dl>
+ <dt>{{DOMxRef("TextDecoder.prototype.encoding")}}{{ReadOnlyInline}}</dt>
+ <dd><font><font>{{DOMxRef("DOMString")}}所包含的解码器的名称,表示</font></font><code>TextDecoder</code><font><font>所使用的解码方法的字符串。</font></font></dd>
+ <dt><font>{{DOMxRef("TextDecoder.prototype.fatal")}}{{ReadOnlyInline}}</font></dt>
+ <dd><font><font><font>布尔值,{{jsxref('Boolean')}},是否显示致命错误。</font></font></font></dd>
+ <dt><font><font><font>{{DOMxRef("TextDecoder.prototype.ignoreBOM")}} {{ReadOnlyInline}}</font></font></font></dt>
+ <dd><font><font><font>布尔值,{{jsxref('Boolean')}},是否忽略 BOM(</font></font></font>byte order marker<font><font><font>)标记。</font></font></font></dd>
+</dl>
+
+<h2 id="方法"><font><font><font>方法</font></font></font></h2>
+
+<dl>
+</dl>
+
+<p><font><em><em><code>TextDecoder</code><font><font> 接口不继承任何</font></font></em><font><font>方法</font></font></em><font><font>。</font></font></font></p>
+
+<dl>
+ <dt><font>{{DOMxRef("TextDecoder.prototype.decode()")}}</font></dt>
+ <dd><font><font><font>返回一个{{DOMxRef("DOMString")}},其中包含使用特定 </font></font><code>TextDecoder</code><font><font> 对象</font><font>的方法解码的文本</font><font>。</font></font></font></dd>
+</dl>
+
+<h2 id="规范"><font>规范</font></h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col"><font><font><font>规格</font></font></font></th>
+ <th scope="col"><font><font><font>状态</font></font></font></th>
+ <th scope="col"><font><font><font>评论</font></font></font></th>
+ </tr>
+ <tr>
+ <td><font>{{SpecName("Encoding", "#interface-textdecoder", "TextDecoder")}}</font></td>
+ <td><font>{{Spec2("Encoding")}}</font></td>
+ <td><font><font><font>初始定义。</font></font></font></td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性"><font><font><font>浏览器兼容性</font></font></font></h2>
+
+<div>
+<div class="hidden"><font><font><font>此页面上的兼容性表是根据结构化数据生成的。</font><font>如果您想提供数据,请查看</font></font><a href="https://github.com/mdn/browser-compat-data"><font><font>https://github.com/mdn/browser-compat-data</font></font></a><font><font>并向我们​​发送请求。</font></font></font></div>
+
+<p><font>{{Compat("api.TextDecoder")}}</font></p>
+</div>
+
+<h2 id="相关链接"><font><font><font>相关链接</font></font></font></h2>
+
+<ul>
+ <li><font>{{DOMxRef("TextEncoder")}} <font><font>接口描述了逆操作。</font></font></font></li>
+ <li><font><a href="/en-US/Add-ons/Code_snippets/StringView"><code>StringView</code></a><font><font> –基于类型数组的字符串的类似C的表示形式</font></font></font></li>
+ <li><font><font><font>一个</font></font><a href="http://code.google.com/p/stringencoding/"><font><font>垫片</font></font></a><font><font>,允许在不支持它的浏览器使用这个接口。</font></font></font></li>
+ <li><font><code><a href="/en-US/docs/Components.utils.importGlobalProperties">Components.utils.importGlobalProperties</a></code></font></li>
+ <li><font><a href="https://nodejs.org/api/util.html#util_class_util_textdecoder"><font><font>Node.js支持从v11.0.0全局导出</font></font></a></font></li>
+</ul>