语法高亮,滚动条美化,设置页面调整

This commit is contained in:
fofolee
2019-04-19 02:41:09 +08:00
parent 1e8f76c000
commit 359d29ee0b
1590 changed files with 12328 additions and 11441 deletions

View File

@@ -5,7 +5,7 @@
<div><iframe class="interactive interactive-js" frameborder="0" height="250" src="https://interactive-examples.mdn.mozilla.net/pages/js/json-parse.html" width="100%"></iframe></div>
<p class="hidden">The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone <a class="external" href="https://github.com/mdn/interactive-examples" rel="noopener">https://github.com/mdn/interactive-examples</a> and send us a pull request.</p>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre class="syntaxbox">JSON.parse(<em>text</em>[, <em>reviver</em>])</pre>
<pre><code class="language-javascript">JSON.parse(<em>text</em>[, <em>reviver</em>])</code></pre>
<h3 id="Parameters" name="Parameters">参数</h3>
<dl>
<dt><code>text</code></dt>
@@ -19,18 +19,18 @@
<p>若传入的字符串不符合 JSON 规范,则会抛出 <a href="Reference/Global_Objects/SyntaxError" title="SyntaxError 对象代表尝试解析语法上不合法的代码的错误。"><code>SyntaxError</code></a> 异常。</p>
<h2 id="Examples" name="Examples">示例</h2>
<h3 id="Example:_Using_JSON.parse" name="Example:_Using_JSON.parse">使用 <code>JSON.parse()</code></h3>
<pre class="brush: js">JSON.parse('{}'); // {}
<pre><code class="language-javascript">JSON.parse('{}'); // {}
JSON.parse('true'); // true
JSON.parse('"foo"'); // "foo"
JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
JSON.parse('null'); // null
JSON.parse('1'); // 1
</pre>
</code></pre>
<p> </p>
<h3 id="Example:_Using_the_reviver_parameter" name="Example:_Using_the_reviver_parameter">使用 <code>reviver</code> 函数</h3>
<p>如果指定了 <code>reviver</code> 函数,则解析出的 JavaScript 值(解析值)会经过一次转换后才将被最终返回(返回值)。更具体点讲就是:解析值本身以及它所包含的所有属性,会按照一定的顺序(从最最里层的属性开始,一级级往外,最终到达顶层,也就是解析值本身)分别的去调用 <code>reviver</code> 函数,在调用过程中,当前属性所属的对象会作为 <code>this</code> 值,当前属性名和属性值会分别作为第一个和第二个参数传入 <code>reviver</code> 中。如果 <code>reviver</code> 返回 <code>undefined</code>,则当前属性会从所属对象中删除,如果返回了其他值,则返回的值会成为当前属性新的属性值。</p>
<p>当遍历到最顶层的值(解析值)时,传入 <code>reviver</code> 函数的参数会是空字符串 <code>""</code>(因为此时已经没有真正的属性)和当前的解析值(有可能已经被修改过了),当前的 <code>this</code> 值会是 <code>{"": 修改过的解析值}</code>,在编写 <code>reviver</code> 函数时,要注意到这个特例。(这个函数的遍历顺序依照:从最内层开始,按照层级顺序,依次向外遍历)</p>
<pre class="brush: js">JSON.parse('{"p": 5}', function (k, v) {
<pre><code class="language-javascript">JSON.parse('{"p": 5}', function (k, v) {
if(k === '') return v; // 如果到了最顶层,则直接返回属性值,
return v * 2; // 否则将属性值变为原来的 2 倍。
}); // { p: 10 }
@@ -48,12 +48,12 @@ JSON.parse('{"1": 1, "2": 2,"3": {"4": 4, "5": {"6": 6}}}', function (k, v) {
// 5
// 3
// ""
</pre>
</code></pre>
<h3 id="JSON.parse()_不允许用逗号作为结尾"><code>JSON.parse()</code> 不允许用逗号作为结尾</h3>
<pre class="example-bad brush: js">// both will throw a SyntaxError
JSON.parse("[1, 2, 3, 4, ]");
JSON.parse('{"foo" : 1, }');
</pre>
</code></pre>
<h2 id="Specifications" name="Specifications">规范</h2>
<table class="standard-table">
<tbody>