diff options
Diffstat (limited to 'files/zh-cn/web/javascript/reference/errors')
78 files changed, 5505 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/errors/already_has_pragma/index.html b/files/zh-cn/web/javascript/reference/errors/already_has_pragma/index.html new file mode 100644 index 0000000000..28bf001e59 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/already_has_pragma/index.html @@ -0,0 +1,37 @@ +--- +title: 'Warning: -file- is being assigned a //# sourceMappingURL, but already has one' +slug: Web/JavaScript/Reference/Errors/Already_has_pragma +translation_of: Web/JavaScript/Reference/Errors/Already_has_pragma +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="消息">消息</h2> + +<pre class="syntaxbox">Warning: -file- is being assigned a //# sourceMappingURL, but already has one.</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>一个警告。JavaScript 的执行不会中止。</p> + +<h2 id="哪里有问题?">哪里有问题?</h2> + +<p>对于给定的 JavaScript 源码,源码映射规定了不止一次。</p> + +<p>JavaScript 源码通常被组合和压缩,使其从服务器传递更加高效。 使用了<a href="http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/">源码映射</a>,调试器能够将执行的源码映射到原始的源码。 有两种指派源码映射的方式,通过注释,或者对 JavaScript 设置标题。</p> + +<h2 id="示例">示例</h2> + +<p>使用文件中的注释来设置源码映射:</p> + +<pre class="brush: js example-good">//# sourceMappingURL=http://example.com/path/to/your/sourcemap.map</pre> + +<p>或者,你可以对你的 JavaScript 文件设置一个标题:</p> + +<pre class="brush: js example-good">X-SourceMap: /path/to/file.js.map</pre> + +<h2 id="另见">另见</h2> + +<ul> + <li><a href="/en-US/docs/Tools/Debugger/How_to/Use_a_source_map">如何使用源码映射 – Firefox Tools documentation</a></li> + <li><a href="http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/">源码映射简介 – HTML5 rocks</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/array_sort_argument/index.html b/files/zh-cn/web/javascript/reference/errors/array_sort_argument/index.html new file mode 100644 index 0000000000..1053ea058b --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/array_sort_argument/index.html @@ -0,0 +1,47 @@ +--- +title: 'TypeError: invalid Array.prototype.sort argument' +slug: Web/JavaScript/Reference/Errors/Array_sort_argument +tags: + - Errors + - JavaScript + - TypeError +translation_of: Web/JavaScript/Reference/Errors/Array_sort_argument +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="信息">信息</h2> + +<pre class="syntaxbox">TypeError: invalid Array.prototype.sort argument (Firefox) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("TypeError")}}</p> + +<h2 id="哪里出错了">哪里出错了?</h2> + +<p>{{jsxref("Array.prototype.sort()")}} 的参数预期为 {{jsxref("undefined")}} 或者是一个比较操作数的函数。</p> + +<h2 id="示例">示例</h2> + +<h3 id="无效的">无效的</h3> + +<pre class="brush: js example-bad">[1, 3, 2].sort(5); // TypeError + +var cmp = { asc: (x, y) => x >= y, dsc : (x, y) => x <= y }; +[1, 3, 2].sort(cmp[this.key] || 'asc'); // TypeError +</pre> + +<h3 id="有效的">有效的</h3> + +<pre class="brush: js example-good">[1, 3, 2].sort(); // [1, 2, 3] + + +var cmp = { asc: (x, y) => x >= y, dsc : (x, y) => x <= y }; +[1, 3, 2].sort(cmp[this.key || 'asc']); // [1, 2, 3]</pre> + +<h2 id="相关">相关</h2> + +<ul> + <li>{{jsxref("Array.prototype.sort()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/bad_octal/index.html b/files/zh-cn/web/javascript/reference/errors/bad_octal/index.html new file mode 100644 index 0000000000..2f9b5d477e --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/bad_octal/index.html @@ -0,0 +1,55 @@ +--- +title: 'SyntaxError: "x" is not a legal ECMA-262 octal constant' +slug: Web/JavaScript/Reference/Errors/Bad_octal +tags: + - Errors + - JavaScript + - SyntaxError + - 严格模式 +translation_of: Web/JavaScript/Reference/Errors/Bad_octal +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="信息">信息</h2> + +<pre class="syntaxbox">Warning: SyntaxError: 08 is not a legal ECMA-262 octal constant. +Warning: SyntaxError: 09 is not a legal ECMA-262 octal constant. +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>仅在 <a href="/zh-CN/docs/Web/JavaScript/Reference/Strict_mode">strict mode</a> 下出现 {{jsxref("SyntaxError")}} 警告。</p> + +<h2 id="哪里出错了">哪里出错了?</h2> + +<p>十进制字面量可以以零作为开始(<code>0</code>),后面跟着其他十进制数,但是假如前导 0 之后的所有数字都小于 8,那么这个数就会被解析为一个八进制的数。因为 08 和 09 不是这样的,所以 JavaScript 会发出警告。</p> + +<p>请注意,不推荐使用八进制字面值和八进制转义序列,并会产生另外的弃用警告。 在 ECMAScript 6 和更高版本里,语法使用前导零后跟小写或大写拉丁字母“O”(0o或0O)。更多信息请查看 <a href="/zh-CN/docs/Web/JavaScript/Reference/Lexical_grammar#Octal">lexical grammar</a>。</p> + +<div class="note"> +<p>注意:现在仅 <strong>firefox</strong> 会产生此错误。</p> +</div> + +<h2 id="示例">示例</h2> + +<h3 id="无效的八进制数">无效的八进制数</h3> + +<pre class="brush: js example-bad">"use strict"; +08; +09; +// SyntaxError: 08 is not a legal ECMA-262 octal constant +// SyntaxError: octal literals and octal escape sequences are deprecated</pre> + +<h3 id="有效的八进制数">有效的八进制数</h3> + +<p>Use a leading zero followed by the letter "o";</p> + +<pre class="brush: js example-good">0O755; +0o644; +</pre> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li><a href="/zh-CN/docs/Web/JavaScript/Reference/Lexical_grammar#Octal">Lexical grammar</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/bad_radix/index.html b/files/zh-cn/web/javascript/reference/errors/bad_radix/index.html new file mode 100644 index 0000000000..1c45f8b6dd --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/bad_radix/index.html @@ -0,0 +1,61 @@ +--- +title: 'RangeError: radix must be an integer' +slug: Web/JavaScript/Reference/Errors/Bad_radix +tags: + - JavaScript + - 范围错误 + - 错误 +translation_of: Web/JavaScript/Reference/Errors/Bad_radix +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="消息">消息</h2> + +<pre class="syntaxbox">RangeError: radix must be an integer at least 2 and no greater than 36 (Firefox) +RangeError: toString() radix argument must be between 2 and 36 (Chrome) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("RangeError")}}</p> + +<h2 id="发生了什么错误?">发生了什么错误?</h2> + +<p>在使用{{jsxref("Number.prototype.toString()")}}方法时使用了可选的基数参数,参数应该为一个2到36之间的整型(数字),返回对应数字的转换为字符串时表示的该进制对应的数字量。</p> + +<p>为什么小于36呢?因为一个大于(包含等于)10的基数在使用时需要用一个字母表字符来代替。不能超过36是因为拉丁字母表中只有26个字符。</p> + +<p>你可能会用到以下的常见基数:</p> + +<ul> + <li>2 for <a href="https://en.wikipedia.org/wiki/Binary_number">二进制</a>,</li> + <li>8 for <a href="https://en.wikipedia.org/wiki/Octal">八进制</a>,</li> + <li>10 for <a href="https://en.wikipedia.org/wiki/Decimal">十进制</a>,</li> + <li>16 for <a href="https://en.wikipedia.org/wiki/Hexadecimal">十六进制</a>.</li> +</ul> + +<h2 id="示例">示例</h2> + +<h3 id="错误示例">错误示例</h3> + +<pre class="brush: js example-bad">(42).toString(0); +(42).toString(1); +(42).toString(37); +(42).toString(150); +// You cannot use a string like this for formatting: +(12071989).toString("MM-dd-yyyy"); +</pre> + +<h3 id="正确示例">正确示例</h3> + +<pre class="brush: js example-good">(42).toString(2); // "101010" (binary) +(13).toString(8); // "15" (octal) +(0x42).toString(10); // "66" (decimal) +(100000).toString(16) // "186a0" (hexadecimal) +</pre> + +<h2 id="参考">参考</h2> + +<ul> + <li>{{jsxref("Number.prototype.toString()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/bad_regexp_flag/index.html b/files/zh-cn/web/javascript/reference/errors/bad_regexp_flag/index.html new file mode 100644 index 0000000000..6365649479 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/bad_regexp_flag/index.html @@ -0,0 +1,104 @@ +--- +title: 'SyntaxError: invalid regular expression flag "x"' +slug: Web/JavaScript/Reference/Errors/Bad_regexp_flag +tags: + - 语法错误 + - 错误 +translation_of: Web/JavaScript/Reference/Errors/Bad_regexp_flag +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="信息">信息</h2> + +<pre class="syntaxbox">SyntaxError: invalid regular expression flag "x" (Firefox) +SyntaxError: Invalid regular expression flags (Chrome) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("SyntaxError","语法错误")}}.</p> + +<h2 id="什么地方出错了">什么地方出错了?</h2> + +<p>在代码中出现了无效的正则表达式的标记。在一个正则表达式字面量中,由闭合的两条斜线组成一个模式,(正则表达式的)标记定义在第二个(斜线)标记之后。他们也可以通过{{jsxref("RegExp", "正则表达式")}} 对象的构造函数(第二个参数)来定义。正则表达式的标记可以单独或者任意次序的组合使用,但ECMAScript只规定了五个。</p> + +<p>要使正则表达式包含标记,使用此语法:</p> + +<pre class="brush: js">var re = /pattern/flags; +</pre> + +<p>或</p> + +<pre class="brush: js">var re = new RegExp('pattern', 'flags');</pre> + +<table class="standard-table"> + <caption>正则表达式标记</caption> + <thead> + <tr> + <th scope="col">标记</th> + <th scope="col">说明</th> + </tr> + </thead> + <tbody> + <tr> + <td><code>g</code></td> + <td>整体检索.</td> + </tr> + <tr> + <td>i</td> + <td>忽略大小写检索.</td> + </tr> + <tr> + <td>m</td> + <td>多行检索.</td> + </tr> + <tr> + <td>u</td> + <td>Unicode; 将模式视为Unicode码点的序列</td> + </tr> + <tr> + <td>y</td> + <td>sticky 检索将从目标字符串的当前位置开始匹配。参阅{{jsxref("RegExp.sticky", "sticky")}}</td> + </tr> + </tbody> +</table> + +<h2 id="示例">示例</h2> + +<p>只有5个有效的正则表达式标记。</p> + +<pre class="brush: js example-bad">/foo/bar; + +// SyntaxError: invalid regular expression flag "b" +</pre> + +<p>你打算创建一个正则表达式吗?一个包含两条斜线的表达式被解释为一个正则表达式的字面量。</p> + +<pre class="brush: js example-bad">let obj = { + url: /docs/Web +}; + +// SyntaxError: invalid regular expression flag "W" +</pre> + +<p>还是你想创建一个字符串呢?添加单引号或双引号创建一个字符串字面量。</p> + +<pre class="brush: js example-good">let obj = { + url: '/docs/Web' +};</pre> + +<h3 id="有效的正则表达式标记">有效的正则表达式标记</h3> + +<p>在JavaScript中允许的五个有效的正则表达式标记,参阅上表。</p> + +<pre class="brush: js example-good">/foo/g; +/foo/gim; +/foo/uy; +</pre> + +<h2 id="相关页面">相关页面</h2> + +<ul> + <li><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Regular_Expressions">正则表达式</a></li> + <li><a href="http://xregexp.com/flags/">XRegEx flags</a> – 正则表达式库提供新的四个标记(<code>n</code>, <code>s</code>, <code>x</code>, <code>A</code>)</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/bad_return_or_yield/index.html b/files/zh-cn/web/javascript/reference/errors/bad_return_or_yield/index.html new file mode 100644 index 0000000000..f2a0fb3de0 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/bad_return_or_yield/index.html @@ -0,0 +1,52 @@ +--- +title: 'SyntaxError: return not in function' +slug: Web/JavaScript/Reference/Errors/Bad_return_or_yield +translation_of: Web/JavaScript/Reference/Errors/Bad_return_or_yield +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="错误信息">错误信息</h2> + +<pre class="syntaxbox">SyntaxError: return not in function +SyntaxError: yield not in function +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("SyntaxError")}}.</p> + +<h2 id="什么地方出错了?">什么地方出错了?</h2> + +<p><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/return">return</a></code> 返回的或者 <code><a href="/en-US/docs/Web/JavaScript/Reference/Operators/yield">yield</a></code> 语句在函数 <a href="/en-US/docs/Web/JavaScript/Guide/Functions">function</a> 外被调用. 或许是少了一个花括号, <code>return</code> 返回的和 <code>yield</code> 语句必须是在一个函数里,因为它们会停止(暂停或恢复)函数的继续执行,然后返回。</p> + +<h2 id="范例">范例</h2> + +<pre class="brush: js example-bad">var cheer = function(score) { + if (score === 147) + return "Maximum!"; + }; + if (score > 100) { + return "Century!"; + } +} + +// SyntaxError: return not in function +// 语法错误:不是在函数里返回</pre> + +<p>初次看好像没什么错误,但是上面这段代码在第一个 if 后面少了一个 “ { ”。正确的应该如下:</p> + +<pre class="brush: js example-good">var cheer = function(score) { + if (score === 147) { + return "Maximum!"; + } + if (score > 100) { + return "Century!"; + } +};</pre> + +<h2 id="相关信息">相关信息</h2> + +<ul> + <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/return">return</a></code></li> + <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Operators/yield">yield</a></code></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/called_on_incompatible_type/index.html b/files/zh-cn/web/javascript/reference/errors/called_on_incompatible_type/index.html new file mode 100644 index 0000000000..fa6c631c6e --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/called_on_incompatible_type/index.html @@ -0,0 +1,63 @@ +--- +title: X.prototype.y called on incompatible type +slug: Web/JavaScript/Reference/Errors/Called_on_incompatible_type +tags: + - JavaScript + - 类型错误 +translation_of: Web/JavaScript/Reference/Errors/Called_on_incompatible_type +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="错误提示">错误提示</h2> + +<pre class="syntaxbox">TypeError: Function.prototype.toString called on incompatible object (Firefox) +TypeError: Function.prototype.bind called on incompatible target (Firefox) +TypeError: Method Set.prototype.add called on incompatible receiver undefined (Chrome) +TypeError: Bind must be called on a function (Chrome) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("TypeError")}}</p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p>当这个错误被抛出时,属于某个对象的函数被调用,但是没有提供给 <code>this</code> 与其所期望的函数类型一致的参数。</p> + +<p>在调用 {{jsxref("Function.prototype.call()")}} 或{{jsxref("Function.prototype.apply()")}} 方法,但是为 <code>this</code> 提供的绑定对象与预期的不匹配时,这个问题就会出现。</p> + +<p>该问题还会出现于将一个(存储在一个对象中的)函数提供给另外一个函数作为参数时。在这种情况下,函数所在的对象并非该函数的 <code>this</code> 绑定的目标对象。为了解决这个问题,你或者为其提供一个 lambda 表达式,该表达式完成函数要完成的任务,或者调用 {{jsxref("Function.prototype.bind()")}} 函数为 <code>this</code> 强制绑定期望的对象。</p> + +<h2 id="示例">示例</h2> + +<h3 id="错误示例">错误示例</h3> + +<pre class="brush: js example-bad">var mySet = new Set; +['bar', 'baz'].forEach(mySet.add); +// mySet.add is a function, but "mySet" is not captured as this. + +var myFun = function () {}; +['bar', 'baz'].forEach(myFun.bind); +// myFun.bind is a function, but "myFun" is not captured as this. + +</pre> + +<h3 id="正确示例">正确示例</h3> + +<pre class="brush: js example-good">var mySet = new Set; +['bar', 'baz'].forEach(mySet.add.bind(mySet)); +// This works due to binding "mySet" as this. + +var myFun = function () {}; +['bar', 'baz'].forEach(x => myFun.bind(x)); +// This works using the "bind" function. It creates a lambda forwarding the argument. + +</pre> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li>{{jsxref("Function.prototype.call()")}}</li> + <li>{{jsxref("Function.prototype.apply()")}}</li> + <li>{{jsxref("Function.prototype.bind()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/cant_access_lexical_declaration_before_init/index.html b/files/zh-cn/web/javascript/reference/errors/cant_access_lexical_declaration_before_init/index.html new file mode 100644 index 0000000000..c99aba5c30 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/cant_access_lexical_declaration_before_init/index.html @@ -0,0 +1,56 @@ +--- +title: 'ReferenceError: can''t access lexical declaration`X'' before initialization' +slug: Web/JavaScript/Reference/Errors/Cant_access_lexical_declaration_before_init +tags: + - JavaScript + - 错误提示 +translation_of: Web/JavaScript/Reference/Errors/Cant_access_lexical_declaration_before_init +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="消息提示">消息提示</h2> + +<pre class="syntaxbox">ReferenceError: assignment to undeclared variable "x" (Firefox) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("ReferenceError")}}</p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p>词法变量在初始化之前被访问。该错误可以发生于任何语句块中,当使用 let 或 const 修饰的变量在初始化之前被访问的时候。</p> + +<h2 id="示例">示例</h2> + +<h3 id="非法情况">非法情况</h3> + +<p>在这个例子中,变量 "foo" 在语句块中再次声明,导致未初始化。</p> + +<pre class="brush: js example-bad">function test(){ + let foo = 33; + if (true) { + let foo = (foo + 55); // ReferenceError: can't access lexical declaration `foo' before initialization + } +} +test(); +</pre> + +<h3 id="正确情况">正确情况</h3> + +<p>在 if 语句块中修改变量 "foo" 的值,不应该在其中进行二次声明。</p> + +<pre class="brush: js example-good">function test(){ + let foo = 33; + if (true) { + foo = (foo + 55); + } +} +test(); +</pre> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Statements/let#Temporal_dead_zone_and_errors_with_let">let 的暂存死区及相关错误</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/cant_access_property/index.html b/files/zh-cn/web/javascript/reference/errors/cant_access_property/index.html new file mode 100644 index 0000000000..8f8d830d2c --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/cant_access_property/index.html @@ -0,0 +1,55 @@ +--- +title: 'TypeError: can''t access property "x" of "y"' +slug: Web/JavaScript/Reference/Errors/Cant_access_property +translation_of: Web/JavaScript/Reference/Errors/Cant_access_property +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="消息">消息</h2> + +<pre class="syntaxbox">TypeError: Unable to get property {x} of undefined or null reference (Edge) +TypeError: can't access property {x} of {y} (Firefox) +TypeError: {y} is undefined, can't access property {x} of it (Firefox) +TypeError: {y} is null, can't access property {x} of it (Firefox) + +Examples: +TypeError: x is undefined, can't access property "prop" of it +TypeError: x is null, can't access property "prop" of it +TypeError: can't access property "prop" of undefined +TypeError: can't access property "prop" of null +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("TypeError")}}.</p> + +<h2 id="哪里出错了">哪里出错了?</h2> + +<p>访问了 {{jsxref("undefined")}} 或{{jsxref("null")}} 值的属性.</p> + +<h2 id="例子">例子</h2> + +<h3 id="无效情形">无效情形</h3> + +<pre class="brush: js example-bad">// 对于undefined 以及 null 值, substring 方法无法正常工作 +var foo = undefined; +foo.substring(1); // TypeError: x is undefined, can't access property "substring" of it + +var foo = null; +foo.substring(1); // TypeError: x is null, can't access property "substring" of it +</pre> + +<h3 id="解决问题">解决问题</h3> + +<p>为了解决<code>undefined</code> 或 <code>null</code> 值的空指针问题,你可以使用 <a href="/en-US/docs/Web/JavaScript/Reference/Operators/typeof">typeof</a> 操作符, 比如.</p> + +<pre class="brush: js">if (typeof foo !== 'undefined') { + // 现在已经确认foo已定义,可以进一步操作. +}</pre> + +<h2 id="参考更多">参考更多</h2> + +<ul> + <li>{{jsxref("undefined")}}</li> + <li>{{jsxref("null")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/cant_define_property_object_not_extensible/index.html b/files/zh-cn/web/javascript/reference/errors/cant_define_property_object_not_extensible/index.html new file mode 100644 index 0000000000..6d3d074a8e --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/cant_define_property_object_not_extensible/index.html @@ -0,0 +1,66 @@ +--- +title: 'TypeError: can''t define property "x": "obj" is not extensible' +slug: Web/JavaScript/Reference/Errors/Cant_define_property_object_not_extensible +tags: + - Error + - Errors + - JavaScript + - TypeError + - 类型错误 + - 错误提示 +translation_of: Web/JavaScript/Reference/Errors/Cant_define_property_object_not_extensible +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="错误提示">错误提示</h2> + +<pre class="syntaxbox">TypeError: can't define property "x": "obj" is not extensible (Firefox) +TypeError: Cannot define property: "x", object is not extensible. (Chrome) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("TypeError")}}</p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p>通常情况下,对象是可以进行扩展的,即可以向其添加新的属性。然而当使用 {{jsxref("Object.preventExtensions()")}} 将一个对象标记为不再可以扩展的情况下,就无法对该对象添加在其被标记为不可扩展之前所拥有的属性之外的新属性了。</p> + +<h2 id="示例">示例</h2> + +<p>在<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode">严格模式</a>下,向已经标记为不可扩展的对象添加新属性会报 TypeError 错误。而在非严格模式下,添加属性 “x” 会被静默忽略。</p> + +<pre class="brush: js example-bad">'use strict'; + +var obj = {}; +Object.preventExtensions(obj); + +obj.x = 'foo'; +// TypeError: can't define property "x": "obj" is not extensible +</pre> + +<p>在<a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">严格模式</a>和非严格模式下两种模式下,调用{{jsxref("Object.defineProperty()")}} 向标记为不可扩展的对象添加新属性都会报 TypeError 错误。</p> + +<pre class="brush: js example-bad">var obj = { }; +Object.preventExtensions(obj); + +Object.defineProperty(obj, + 'x', { value: "foo" } +); +// TypeError: can't define property "x": "obj" is not extensible +</pre> + +<p>为了修复这个错误,你可以彻底移除 {{jsxref("Object.preventExtensions()")}} 语句,或者将其移动位置,使得属性在对象被标记为不可扩展之前添加。当然如果不需要试图添加的属性的话,你也可以将其移除。</p> + +<pre class="brush: js example-good">'use strict'; + +var obj = {}; +obj.x = 'foo'; // add property first and only then prevent extensions + +Object.preventExtensions(obj);</pre> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li>{{jsxref("Object.preventExtensions()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/cant_delete/index.html b/files/zh-cn/web/javascript/reference/errors/cant_delete/index.html new file mode 100644 index 0000000000..d92956f409 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/cant_delete/index.html @@ -0,0 +1,58 @@ +--- +title: 'TypeError: property "x" is non-configurable and can''t be deleted' +slug: Web/JavaScript/Reference/Errors/Cant_delete +tags: + - JavaScript + - 严格模式 + - 类型错误 + - 错误 +translation_of: Web/JavaScript/Reference/Errors/Cant_delete +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="消息">消息</h2> + +<pre class="syntaxbox">TypeError: property "x" is non-configurable and can't be deleted. (Firefox) +TypeError: Cannot delete property 'x' of #<Object> (Chrome) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("TypeError")}} 只出现在严格模式下。</p> + +<h2 id="哪里有问题?">哪里有问题?</h2> + +<p>尝试删除某个属性,但是这个属性是 <a href="/en-US/docs/Web/JavaScript/Data_structures#Properties">不可配置的</a>。<code>configurable</code>属性控制是否该属性能从对象上删除,以及它的属性(除了<code>writable</code>)能否被修改。</p> + +<p>这个错误仅仅在<a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">严格模式</a>下出现。在非严格模式下,这个操作返回 <code>false</code>。</p> + +<h2 id="示例">示例</h2> + +<p>不可配置的属性并不特别常见,但是它们可以使用 {{jsxref("Object.defineProperty()")}} 或 {{jsxref("Object.freeze()")}} 创建。</p> + +<pre class="brush: js example-bad">'use strict'; +var obj = Object.freeze({name: 'Elsa', score: 157}); +delete obj.score; // TypeError + +'use strict'; +var obj = {}; +Object.defineProperty(obj, 'foo', {value: 2, configurable: false}); +delete obj.foo; // TypeError + +'use strict'; +var frozenArray = Object.freeze([0, 1, 2]); +frozenArray.pop(); // TypeError +</pre> + +<p>也有一些内建于 JavaScript 的不可配置属性。你可能会尝试删除一个数学常量。</p> + +<pre class="brush: js example-bad">'use strict'; +delete Math.PI; // TypeError</pre> + +<h2 id="另见">另见</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/delete">delete operator</a></li> + <li>{{jsxref("Object.defineProperty()")}}</li> + <li>{{jsxref("Object.freeze()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/cant_redefine_property/index.html b/files/zh-cn/web/javascript/reference/errors/cant_redefine_property/index.html new file mode 100644 index 0000000000..abba81b6f2 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/cant_redefine_property/index.html @@ -0,0 +1,50 @@ +--- +title: 'TypeError: can''t redefine non-configurable property "x"' +slug: Web/JavaScript/Reference/Errors/Cant_redefine_property +tags: + - JavaScript + - 类型错误 + - 错误提示 +translation_of: Web/JavaScript/Reference/Errors/Cant_redefine_property +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="错误提示">错误提示</h2> + +<pre class="syntaxbox">TypeError: can't redefine non-configurable property "x" (Firefox) +TypeError: Cannot redefine property: "x" (Chrome) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("TypeError")}}</p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p>这种错误的起因在于试图给对象重新定义一个属性,但是该属性是<a href="/en-US/docs/Web/JavaScript/Data_structures#Properties">不可配置的</a>。 <code>configurable</code> 特性控制着该属性是否可以从对象中删除,以及它的各个特性(除 writable 之外)是否可以修改。通常使用<a href="/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer">对象初始化语句</a>定义的对象属性是可配置的。而使用 {{jsxref("Object.defineProperty()")}} 定义的属性则默认不可配置。</p> + +<h2 id="示例">示例</h2> + +<h3 id="使用_object.defineProperty_创建的不可配置属性">使用 object.defineProperty 创建的不可配置属性</h3> + +<p>在使用 {{jsxref("Object.defineProperty()")}} 创建属性的时候,如果没有明确将其设定为可配置的,那么创建出来的属性就是不可配置的。</p> + +<pre class="brush: js example-bad">var obj = Object.create({}); +Object.defineProperty(obj, "foo", {value: "bar"}); + +Object.defineProperty(obj, "foo", {value: "baz"}); +// TypeError: can't redefine non-configurable property "foo" +</pre> + +<p>如果想要稍后重新定义的话,那么需要将 "foo" 属性设置为可配置的。</p> + +<pre class="brush: js example-good">var obj = Object.create({}); +Object.defineProperty(obj, "foo", {value: "bar", configurable: true}); +Object.defineProperty(obj, "foo", {value: "baz", configurable: true});</pre> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Data_structures#Properties">[[Configurable]]</a></li> + <li>{{jsxref("Object.defineProperty()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/cyclic_object_value/index.html b/files/zh-cn/web/javascript/reference/errors/cyclic_object_value/index.html new file mode 100644 index 0000000000..9ab3cb7b02 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/cyclic_object_value/index.html @@ -0,0 +1,76 @@ +--- +title: 'TypeError: cyclic object value' +slug: Web/JavaScript/Reference/Errors/Cyclic_object_value +tags: + - Error + - JavaScript + - 类型错误 + - 错误提示 +translation_of: Web/JavaScript/Reference/Errors/Cyclic_object_value +--- +<div>{{jsSidebar("Errors")}}</div> + +<div>当一段JSON中出现循环引用,使用{{jsxref("JSON.stringify()")}}这个方法去处理JSON时会报这个"cyclic object value"错误。</div> + +<h2 id="提示信息">提示信息</h2> + +<pre class="notranslate">TypeError: cyclic object value (Firefox) +TypeError: Converting circular structure to JSON (Chrome and Opera) +TypeError: Circular reference in value argument not supported (Edge)</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("TypeError")}}</p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p>当调用 {{jsxref("JSON.stringify()")}} 方法去处理循环引用结构的JSON会失败。</p> + +<p>JSON标准参考链接:<a href="https://www.json.org/json-zh.html">JSON format</a></p> + +<h2 id="示例">示例</h2> + +<h3 id="循环引用">循环引用</h3> + +<p>在如下循环结构中:</p> + +<pre class="brush: js notranslate">var a = {}; +var b = {}; +a.child = b; +b.child = a; +</pre> + +<p>{{jsxref("JSON.stringify()")}} 将会报错</p> + +<pre class="brush: js example-bad notranslate">JSON.stringify(a); +// TypeError: cyclic object value +</pre> + +<p>要处理循环引用的JSON,可以使用支持这种结构的库(例如<a href="https://github.com/douglascrockford/JSON-js/blob/master/cycle.js">cycle.js</a>)),或者自己实现。</p> + +<p>下面代码展示了,可以通过指定替换函数({{jsxref("JSON.stringify()")}} 方法的第二个参数) 来检查转换成字符串之前是否有循环对象引用的存在。</p> + +<p>注意:以下代码并不会保存循环引用的值。</p> + +<pre class="brush: js example-good notranslate">var seen = []; + +var replacer = function(key, value) { + if (typeof value === "object" && value !== null) { + if (seen.indexOf(value) >= 0) { + return; + } + seen.push(value); + } + return value; +}; + +JSON.stringify(a, replacer); +// "{"child":{}}" +</pre> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li>{{jsxref("JSON.stringify")}}</li> + <li><a href="https://github.com/douglascrockford/JSON-js/blob/master/cycle.js">cycle.js</a> – 提出了两个方法<code>JSON.decycle</code> 和 <code>JSON.retrocycle</code>,这两个方法能够对循环对象引用结构进行编码和解码,并且使之成为一种扩展的且向下兼容的JSON格式。</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/dead_object/index.html b/files/zh-cn/web/javascript/reference/errors/dead_object/index.html new file mode 100644 index 0000000000..c974ec27b8 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/dead_object/index.html @@ -0,0 +1,49 @@ +--- +title: 'TypeError: can''t access dead object' +slug: Web/JavaScript/Reference/Errors/Dead_object +tags: + - JavaScript + - 插件 + - 错误提示 +translation_of: Web/JavaScript/Reference/Errors/Dead_object +--- +<div>{{JSSidebar("Errors")}}</div> + +<h2 id="错误提示">错误提示</h2> + +<pre class="syntaxbox">TypeError: can't access dead object +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("TypeError")}}</p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p>为了提高内存使用效率以及防止内存泄露,Firefox 浏览器不允许插件在 DOM 所在的父页面被销毁后对 DOM 对象保持强引用。死对象指的是在 DOM 被销毁后依然持有对 DOM 元素的强引用(处于活跃状态)。为了避免这样的问题,对处于外部文档中的 DOM 节点的引用应该被存储于一个专属于那个文档的对象当中,并且在文档卸载的时候将其清理,或者使用<a href="/en-US/docs/Mozilla/Tech/XPCOM/Language_Bindings/Components.utils.getWeakReference">弱引用</a>方式进行存储。</p> + +<h2 id="Checking_if_an_object_is_dead">Checking if an object is dead</h2> + +<p><a href="/en-US/docs/Mozilla/Tech/XPCOM/Language_Bindings/Components.utils">Components.utils</a> offers a <code>isDeadWrapper()</code> method, which privileged code might use.</p> + +<pre class="brush: js">if (Components.utils.isDeadWrapper(window)) { + // dead +}</pre> + +<p>Unprivileged code has no access to Component.utils and might just be able catch the exception.</p> + +<pre class="brush: js">try { + String(window); +} +catch (e) { + console.log("window is likely dead"); +}</pre> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li><a href="https://blog.mozilla.org/addons/2012/09/12/what-does-cant-access-dead-object-mean/">What does “can’t access dead object” mean?</a></li> + <li><a href="/en-US/docs/Extensions/Common_causes_of_memory_leaks_in_extensions">Common causes of memory leaks in extensions</a></li> + <li><a href="/en-US/docs/Mozilla/Tech/XPCOM/Language_Bindings/Components.utils">Components.utils</a></li> + <li><a href="/en-US/docs/Mozilla/Zombie_compartments">Zombie Compartments</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/delete_in_strict_mode/index.html b/files/zh-cn/web/javascript/reference/errors/delete_in_strict_mode/index.html new file mode 100644 index 0000000000..aa9a3293ae --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/delete_in_strict_mode/index.html @@ -0,0 +1,69 @@ +--- +title: >- + SyntaxError: applying the 'delete' operator to an unqualified name is + deprecated +slug: Web/JavaScript/Reference/Errors/Delete_in_strict_mode +tags: + - JavaScript + - 语法错误 + - 错误提示 +translation_of: Web/JavaScript/Reference/Errors/Delete_in_strict_mode +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="错误提示">错误提示</h2> + +<pre class="syntaxbox">SyntaxError: applying the 'delete' operator to an unqualified name is deprecated (Firefox) +SyntaxError: Delete of an unqualified identifier in strict mode. (Chrome) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("SyntaxError")}} 仅出现在<a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">严格模式</a>下。</p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p>在 JavaScript 中,普通变量是不能通过 <code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete">delete</a></code> 操作符来删除的。在严格模式下,试图去删除一个变量会报错,这是不允许的。</p> + +<p>delete 操作符只能用于删除对象中的属性。只有可配置的对象属性才“符合”被删除的条件。</p> + +<p>与一般流行的观点相反的是, delete 操作符与直接释放内存<strong>无关</strong>。内存管理是通过切断引用来间接实现的。可以参考<a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Memory_Management">内存管理</a>页面与 <a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/delete">delete </a>操作符页面来获取更多的细节信息。</p> + +<p>这个错误提示只出现于<a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">严格模式</a>。在非严格模式下,该操作返回 false。</p> + +<h2 id="示例">示例</h2> + +<p>在 JavaScript 中,普通变量是不能删除的,在严格模式下会报错:</p> + +<pre class="brush: js example-bad">'use strict'; + +var x; + +// ... + +delete x; + +// SyntaxError: applying the 'delete' operator to an unqualified name +// is deprecated +</pre> + +<p>要释放变量引用的内容,可以将变量值设置为 {{jsxref("null")}}:</p> + +<pre class="brush: js example-good">'use strict'; + +var x; + +// ... + +x = null; + +// x can be garbage collected +</pre> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Operators/delete">delete</a></code></li> + <li><a href="/en-US/docs/Web/JavaScript/Memory_Management">内存管理</a></li> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Errors/Cant_delete">TypeError: property "x" is non-configurable and can't be deleted</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/deprecated_caller_or_arguments_usage/index.html b/files/zh-cn/web/javascript/reference/errors/deprecated_caller_or_arguments_usage/index.html new file mode 100644 index 0000000000..cfafd9c155 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/deprecated_caller_or_arguments_usage/index.html @@ -0,0 +1,76 @@ +--- +title: 'ReferenceError: deprecated caller or arguments usage' +slug: Web/JavaScript/Reference/Errors/Deprecated_caller_or_arguments_usage +tags: + - Errors + - JavaScript + - Strict Mode + - 严格模式 + - 警告 +translation_of: Web/JavaScript/Reference/Errors/Deprecated_caller_or_arguments_usage +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="信息">信息</h2> + +<pre class="syntaxbox">Warning: ReferenceError: deprecated caller usage (Firefox) +Warning: ReferenceError: deprecated arguments usage (Firefox) +TypeError: 'callee' and 'caller' cannot be accessed in strict mode. (Safari) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>仅在严格模式下出现的 {{jsxref("ReferenceError")}} 警告。<span class="short_text" id="result_box" lang="zh-CN"><span>JavaScript 的执行将不会停止。</span></span></p> + +<h2 id="发生了什么">发生了什么?</h2> + +<p>在 <a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">strict mode</a> 中,{{jsxref("Function.caller")}} 和 {{jsxref("Function.arguments")}} 属性是不该使用的。它们都是已经被废弃的了,因为这两者泄露了函数的调用者,是不标准的,难于优化和有这潜在的性能问题。</p> + +<h2 id="实力">实力</h2> + +<h3 id="废弃的_function.caller_or_arguments.callee.caller">废弃的 <code>function.caller</code> or <code>arguments.callee.caller</code></h3> + +<p>{{jsxref("Function.caller")}} 和 <code><a href="/en-US/docs/Web/JavaScript/Reference/Functions/arguments/callee">arguments.callee.caller</a></code> 都是已废弃的 (详见参考文章)。</p> + +<pre class="brush: js example-bad">"use strict"; + +function myFunc() { + if (myFunc.caller == null) { + return 'The function was called from the top!'; + } else { + return 'This function\'s caller was ' + myFunc.caller; + } +} + +myFunc(); +// Warning: ReferenceError: deprecated caller usage +// "The function was called from the top!"</pre> + +<h3 id="Function.arguments"><code>Function.arguments</code></h3> + +<p>{{jsxref("Function.arguments")}} 已被废弃。 (详见参考文章)。</p> + +<pre class="brush: js example-bad">"use strict"; + +function f(n) { g(n - 1); } + +function g(n) { + console.log('before: ' + g.arguments[0]); + if (n > 0) { f(n); } + console.log('after: ' + g.arguments[0]); +} + +f(2); + +console.log('returned: ' + g.arguments); +// Warning: ReferenceError: deprecated arguments usage +</pre> + +<h2 id="相关">相关</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features">Deprecated and obsolete features</a></li> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">Strict mode</a></li> + <li>{{jsxref("Function.arguments")}}</li> + <li>{{jsxref("Function.caller")}} and <code><a href="/en-US/docs/Web/JavaScript/Reference/Functions/arguments/callee">arguments.callee.caller</a></code></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/deprecated_expression_closures/index.html b/files/zh-cn/web/javascript/reference/errors/deprecated_expression_closures/index.html new file mode 100644 index 0000000000..465bd6741b --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/deprecated_expression_closures/index.html @@ -0,0 +1,78 @@ +--- +title: 'Warning: expression closures are deprecated' +slug: Web/JavaScript/Reference/Errors/Deprecated_expression_closures +tags: + - JavaScript + - 警告 +translation_of: Web/JavaScript/Reference/Errors/Deprecated_expression_closures +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="错误提示">错误提示</h2> + +<pre class="syntaxbox">Warning: expression closures are deprecated +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>警告。JavaScript 引擎不会停止运行。</p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p>非标准化的<a href="/en-US/docs/Web/JavaScript/Reference/Operators/Expression_closures">表达式闭包</a>语法(简写函数语法)已被废弃,不应该再被使用。该语法将会在 {{bug(1083458)}} 中全部移除,到时候用到该语法的脚本程序会报 SyntaxError 错误。</p> + +<h2 id="示例">示例</h2> + +<h3 id="被废弃的语法">被废弃的语法</h3> + +<p>表达式闭包从函数声明或者对象方法定义中省略了花括号或是 return 语句。</p> + +<pre class="brush: js example-bad">var x = function() 1; + +var obj = { + count: function() 1 +}; +</pre> + +<h3 id="标准语法">标准语法</h3> + +<p>将非标准化的表达式闭包语法转换成标准 ECMAScript 语法,可以添加花括号以及 return 语句。</p> + +<pre class="brush: js example-good">var x = function() { return 1; } + +var obj = { + count: function() { return 1; } +}; +</pre> + +<h3 id="标准语法之箭头函数">标准语法之箭头函数</h3> + +<p>另外,你可以选择使用箭头函数:</p> + +<pre class="brush: js example-good">var x = () => 1;</pre> + +<h3 id="标准语法之方法的简略写法">标准语法之方法的简略写法</h3> + +<p>表达式闭包还会出现在 getter 与 setter 中,例如:</p> + +<pre class="brush: js example-bad">var obj = { + get x() 1, + set x(v) this.v = v +}; +</pre> + +<p>根据 ES2015 中对于<a href="/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions">方法定义</a>的规定,上述写法可以转换成如下形式:</p> + +<pre class="brush: js example-good">var obj = { + get x() { return 1 }, + set x(v) { this.v = v } +}; +</pre> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Expression_closures">Expression closures</a></li> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions">Arrow functions</a></li> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions">Method definitions</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/deprecated_octal/index.html b/files/zh-cn/web/javascript/reference/errors/deprecated_octal/index.html new file mode 100644 index 0000000000..ca56bc00ac --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/deprecated_octal/index.html @@ -0,0 +1,67 @@ +--- +title: 'SyntaxError: "0"-prefixed octal literals and octal escape seq. are deprecated' +slug: Web/JavaScript/Reference/Errors/Deprecated_octal +tags: + - JavaScript + - 严格模式 + - 语法错误 + - 错误提示 +translation_of: Web/JavaScript/Reference/Errors/Deprecated_octal +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="错误提示">错误提示</h2> + +<pre class="syntaxbox">SyntaxError: +"0"-prefixed octal literals and octal escape sequences are deprecated; +for octal literals use the \"0o\" prefix instead +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>语法错误({{jsxref("SyntaxError")}} ),只出现于<a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">严格模式</a>下。</p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p>八进制字面量与八进制转义序列语法已经被废弃,在严格模式下会报语法错误({{jsxref("SyntaxError")}})。在 ECMAScript 2015 及以后的规范中,标准语法是前导 0 后面跟一个大写或小写的拉丁文字母 "O" (<code>0o</code> 或 <code>0O)。</code></p> + +<h2 id="示例">示例</h2> + +<h3 id="前导0形式的八进制字面量">前导"0"形式的八进制字面量</h3> + +<pre class="brush: js example-bad">"use strict"; + +03; + +// SyntaxError: "0"-prefixed octal literals and octal escape sequences +// are deprecated</pre> + +<h3 id="八进制转义序列">八进制转义序列</h3> + +<pre class="brush: js example-bad">"use strict"; + +"\251"; + +// SyntaxError: "0"-prefixed octal literals and octal escape sequences +// are deprecated +</pre> + +<h3 id="合法的八进制数字">合法的八进制数字</h3> + +<p>使用前导 0 后面跟字母 "o" 或 "O" 的形式:</p> + +<pre class="brush: js example-good">0o3; +</pre> + +<p>至于八进制转义序列,你可以使用十六进制转义序列来代替:</p> + +<pre class="brush: js example-good">'\xA9';</pre> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Octal">Lexical grammar</a></li> + <li> + <p><a href="/en-US/docs/Web/JavaScript/Reference/Errors/Bad_octal">警告: 08/09 不是符合 ECMA-262 规范的八进制常量</a></p> + </li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/deprecated_source_map_pragma/index.html b/files/zh-cn/web/javascript/reference/errors/deprecated_source_map_pragma/index.html new file mode 100644 index 0000000000..e37aec2fa3 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/deprecated_source_map_pragma/index.html @@ -0,0 +1,109 @@ +--- +title: >- + SyntaxError: Using //@ to indicate sourceURL pragmas is deprecated. Use //# + instead +slug: Web/JavaScript/Reference/Errors/Deprecated_source_map_pragma +tags: + - Errors + - JavaScript + - Source maps +translation_of: Web/JavaScript/Reference/Errors/Deprecated_source_map_pragma +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="信息">信息</h2> + +<pre class="syntaxbox">Warning: SyntaxError: Using //@ to indicate sourceURL pragmas is deprecated. Use //# instead + +Warning: SyntaxError: Using //@ to indicate sourceMappingURL pragmas is deprecated. Use //# instead +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("SyntaxError")}} 的警告。不会终止 JavaScript 的执行。</p> + +<h2 id="哪里错了">哪里错了?</h2> + +<p>在 JavaScript 源码中使用了已废弃的 source map 语法。</p> + +<p>JavaScript 源代码经常被组合和压缩,以便能更高效地从服务器获取它们。使用了 <a href="http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/">source maps</a>,调试器就可以将正在执行的代码映射到原始源文件。</p> + +<p>因为 IE 浏览器只要页面在 <code>//@cc_on</code> 之后的都会被IE JScript引擎解释为打开条件编译后,所以 source map 的规范更改了语法。<a href="https://msdn.microsoft.com/en-us/library/8ka90k2e%28v=vs.94%29.aspx">条件编译注释</a> 是 IE 的一个小特色,但是它破坏了 <a href="https://bugs.jquery.com/ticket/13274">jQuery</a> 和其他库的 source map。</p> + +<h2 id="示例">示例</h2> + +<h3 id="废弃的语法">废弃的语法</h3> + +<p>使用 "@" 符号的语法已经被废弃了。</p> + +<pre class="brush: js example-bad">//@ sourceMappingURL=http://example.com/path/to/your/sourcemap.map +</pre> + +<h3 id="标准语法">标准语法</h3> + +<p>使用 "#" 符号代替。</p> + +<pre class="brush: js example-good">//# sourceMappingURL=http://example.com/path/to/your/sourcemap.map</pre> + +<p>或者,您也可以为 JavaScript 文件设置 header,以避免添加注释:</p> + +<pre class="brush: js example-good">X-SourceMap: /path/to/file.js.map</pre> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>New syntax</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{ CompatGeckoDesktop(24) }}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>New syntax</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{ CompatGeckoMobile(24) }}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="相关">相关</h2> + +<ul> + <li><a href="/en-US/docs/Tools/Debugger/How_to/Use_a_source_map">How to use a source map – Firefox Tools documentation</a></li> + <li><a href="http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/">Introduction to source maps – HTML5 rocks</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/deprecated_string_generics/index.html b/files/zh-cn/web/javascript/reference/errors/deprecated_string_generics/index.html new file mode 100644 index 0000000000..dd59c26276 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/deprecated_string_generics/index.html @@ -0,0 +1,104 @@ +--- +title: 'Warning: String.x is deprecated; use String.prototype.x instead' +slug: Web/JavaScript/Reference/Errors/Deprecated_String_generics +tags: + - JavaScript + - 警告 +translation_of: Web/JavaScript/Reference/Errors/Deprecated_String_generics +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="错误提示">错误提示</h2> + +<pre class="syntaxbox">Warning: String.charAt is deprecated; use String.prototype.charAt instead +Warning: String.charCodeAt is deprecated; use String.prototype.charCodeAt instead +Warning: String.concat is deprecated; use String.prototype.concat instead +Warning: String.contains is deprecated; use String.prototype.contains instead +Warning: String.endsWith is deprecated; use String.prototype.endsWith instead +Warning: String.includes is deprecated; use String.prototype.includes instead +Warning: String.indexOf is deprecated; use String.prototype.indexOf instead +Warning: String.lastIndexOf is deprecated; use String.prototype.lastIndexOf instead +Warning: String.localeCompare is deprecated; use String.prototype.localeCompare instead +Warning: String.match is deprecated; use String.prototype.match instead +Warning: String.normalize is deprecated; use String.prototype.normalize instead +Warning: String.replace is deprecated; use String.prototype.replace instead +Warning: String.search is deprecated; use String.prototype.search instead +Warning: String.slice is deprecated; use String.prototype.slice instead +Warning: String.split is deprecated; use String.prototype.split instead +Warning: String.startsWith is deprecated; use String.prototype.startsWith instead +Warning: String.substr is deprecated; use String.prototype.substr instead +Warning: String.substring is deprecated; use String.prototype.substring instead +Warning: String.toLocaleLowerCase is deprecated; use String.prototype.toLocaleLowerCase instead +Warning: String.toLocaleUpperCase is deprecated; use String.prototype.toLocaleUpperCase instead +Warning: String.toLowerCase is deprecated; use String.prototype.toLowerCase instead +Warning: String.toUpperCase is deprecated; use String.prototype.toUpperCase instead +Warning: String.trim is deprecated; use String.prototype.trim instead +Warning: String.trimLeft is deprecated; use String.prototype.trimLeft instead +Warning: String.trimRight is deprecated; use String.prototype.trimRight instead +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>警告。JavaScript 引擎不会停止运行。</p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p>非标准的泛型 {{jsxref("String")}} 方法已经被废弃,将来会被移除(这些方法仅在 Firefox 浏览器中得到实现)。String 泛型在 String 对象上提供了一系列的 String 实例方法,使得这些 String 方法可以应用于任何类型的对象。</p> + +<p>Firefox {{bug(1222552)}} 对 String 泛型方法的移除进行了追踪。</p> + +<h2 id="示例">示例</h2> + +<h3 id="废弃的语法">废弃的语法</h3> + +<pre class="brush: js example-bad">var num = 15; +String.replace(num, /5/, '2');</pre> + +<h3 id="标准语法">标准语法</h3> + +<pre class="brush: js example-good">var num = 15; +String(num).replace(/5/, '2'); +</pre> + +<h2 id="垫片">垫片</h2> + +<p>以下是一个垫片脚本来为不支持 String 泛型方法浏览器提供支持:</p> + +<pre class="brush: js">/*globals define*/ +// Assumes all supplied String instance methods already present +// (one may use shims for these if not available) +(function() { + 'use strict'; + + var i, + // We could also build the array of methods with the following, but the + // getOwnPropertyNames() method is non-shimable: + // Object.getOwnPropertyNames(String).filter(function(methodName) { + // return typeof String[methodName] === 'function'; + // }); + methods = [ + 'contains', 'substring', 'toLowerCase', 'toUpperCase', 'charAt', + 'charCodeAt', 'indexOf', 'lastIndexOf', 'startsWith', 'endsWith', + 'trim', 'trimLeft', 'trimRight', 'toLocaleLowerCase', 'normalize', + 'toLocaleUpperCase', 'localeCompare', 'match', 'search', 'slice', + 'replace', 'split', 'substr', 'concat', 'localeCompare' + ], + methodCount = methods.length, + assignStringGeneric = function(methodName) { + var method = String.prototype[methodName]; + String[methodName] = function(arg1) { + return method.apply(arg1, Array.prototype.slice.call(arguments, 1)); + }; + }; + + for (i = 0; i < methodCount; i++) { + assignStringGeneric(methods[i]); + } +}());</pre> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li>{{jsxref("String")}}</li> + <li>{{jsxref("Global_Objects/Array", "Generics", "#Array_generic_methods", 1)}} 同样存在于 {{jsxref("Array")}} 的方法中(也同样被废弃了)。</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/deprecated_tolocaleformat/index.html b/files/zh-cn/web/javascript/reference/errors/deprecated_tolocaleformat/index.html new file mode 100644 index 0000000000..e8e8040227 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/deprecated_tolocaleformat/index.html @@ -0,0 +1,90 @@ +--- +title: 'Warning: Date.prototype.toLocaleFormat is deprecated' +slug: Web/JavaScript/Reference/Errors/Deprecated_toLocaleFormat +tags: + - JavaScript + - 警告 +translation_of: Web/JavaScript/Reference/Errors/Deprecated_toLocaleFormat +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="错误提示">错误提示</h2> + +<pre class="syntaxbox">Warning: Date.prototype.toLocaleFormat is deprecated; consider using Intl.DateTimeFormat instead +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>警告。JavaScript 引擎不会停止运行。</p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p>{{jsxref("Date.prototype.toLocaleFormat")}} 是非标准化的方法,已经被废弃,不应该再进行使用。该方法需要传入与 C 语言中的strftime() 方法相似的格式化字符串。该实现将会在 {{bug(818634)}} 中完全移除。</p> + +<h2 id="示例">示例</h2> + +<h3 id="废止使用的语法">废止使用的语法</h3> + +<p>{{jsxref("Date.prototype.toLocaleFormat")}} 方法已经废弃,以后会被移除(只在 Firefox 浏览器中有用到,没有在其他浏览器中使用)。</p> + +<pre class="brush: js example-bad">var today = new Date(); +var date = today.toLocaleFormat('%A, %e. %B %Y'); + +console.log(date); +// In German locale +// "Freitag, 10. März 2017"</pre> + +<h3 id="备选标准语法之一:使用_ECMAScript_Intl_API">备选标准语法之一:使用 ECMAScript Intl API </h3> + +<p>ECMA-402 (ECMAScript Intl API) 标准规定了支持不同语言的日期和时间格式化形式的对象和方法(在 Chrome 24+, Firefox 29+, IE11+, Safari10+ 中可用)。</p> + +<p>现在如果只需要格式化一个日期对象的话,那么可以使用 {{jsxref("Date.prototype.toLocaleDateString")}} 方法。</p> + +<pre class="brush: js example-good">var today = new Date(); +var options = { weekday: 'long', year: 'numeric', + month: 'long', day: 'numeric' }; +var date = today.toLocaleDateString('de-DE', options); + +console.log(date); +// "Freitag, 10. März 2017" +</pre> + +<p>或者,可以使用 {{jsxref("DateTimeFormat", "Intl.DateTimeFormat")}} 对象,该对象允许你将格式化器对象缓存起来,省去很多重复性的计算工作,所以格式化操作会很快。这在需要对一系列的日期对象进行格式化的时候非常有用。</p> + +<pre class="brush: js example-good">var options = { weekday: 'long', year: 'numeric', + month: 'long', day: 'numeric' }; +var dateFormatter = new Intl.DateTimeFormat('de-DE', options) + +var dates = [Date.UTC(2012, 11, 20, 3, 0, 0), + Date.UTC(2014, 04, 12, 8, 0, 0)]; + +dates.forEach(date => console.log(dateFormatter.format(date))); + +// "Donnerstag, 20. Dezember 2012" +// "Montag, 12. Mai 2014" +</pre> + +<h3 id="备选标准语法之二:使用_Date_对象的方法">备选标准语法之二:使用 Date 对象的方法</h3> + +<p>{{jsxref("Date")}} 对象提供了一系列的方法来生成定制化的格式化字符串。</p> + +<pre class="brush: js example-bad">(new Date()).toLocaleFormat("%Y%m%d"); +// "20170310" +</pre> + +<p>可以转换为:</p> + +<pre class="brush: js example-good">let now = new Date(); +let date = now.getFullYear() * 10000 + + (now.getMonth() + 1) * 100 + now.getDate(); + +console.log(date); +// "20170310"</pre> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li>{{jsxref("Date.prototype.toLocaleFormat")}}</li> + <li>{{jsxref("Date.prototype.toLocaleDateString")}}</li> + <li>{{jsxref("DateTimeFormat", "Intl.DateTimeFormat")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/equal_as_assign/index.html b/files/zh-cn/web/javascript/reference/errors/equal_as_assign/index.html new file mode 100644 index 0000000000..e92f0a63e8 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/equal_as_assign/index.html @@ -0,0 +1,52 @@ +--- +title: 'SyntaxError: test for equality (==) mistyped as assignment (=)?' +slug: Web/JavaScript/Reference/Errors/Equal_as_assign +tags: + - 语法错误 +translation_of: Web/JavaScript/Reference/Errors/Equal_as_assign +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="消息">消息</h2> + +<pre class="syntaxbox">Warning: SyntaxError: test for equality (==) mistyped as assignment (=)? +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("SyntaxError")}} 只在<a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">严格模式</a>下会出现的警告。</p> + +<h2 id="什么地方出错了">什么地方出错了?</h2> + +<p>在通常期望进行相等判定(<code>==</code>)的地方出现了赋值<code>(=</code>)。 为了帮助调试,JavaScript(在开启严格模式的情况下)会对这种情况进行警告。</p> + +<h2 id="示例">示例</h2> + +<h3 id="条件表达式内的赋值">条件表达式内的赋值</h3> + +<p>不建议在条件表达式中 (例如 <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/if...else">if...else</a></code>) 使用简单赋值语句,因为在扫视代码的时候赋值操作与相等判定容易产生混淆。例如,不要使用以下写法:</p> + +<pre class="brush: js example-bad">if (x = y) { + // do the right thing +} +</pre> + +<p>如果你需要在条件表达式中使用赋值语句, 通常的做法是用一对括号把赋值语句包起来。 例如:</p> + +<pre class="brush: js">if ((x = y)) { + // do the right thing +}</pre> + +<p>否则, 你的本意可能是想用比较操作符 (如 <code>==</code> 或 <code>===</code>):</p> + +<pre class="brush: js">if (x == y) { + // do the right thing +}</pre> + +<h2 id="相关页面">相关页面</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">Strict mode</a></li> + <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/if...else">if...else</a></code></li> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators">Comparison operators</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/for-each-in_loops_are_deprecated/index.html b/files/zh-cn/web/javascript/reference/errors/for-each-in_loops_are_deprecated/index.html new file mode 100644 index 0000000000..351be47a9d --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/for-each-in_loops_are_deprecated/index.html @@ -0,0 +1,171 @@ +--- +title: 'Warning: JavaScript 1.6''s for-each-in loops are deprecated' +slug: Web/JavaScript/Reference/Errors/For-each-in_loops_are_deprecated +tags: + - Warning +translation_of: Web/JavaScript/Reference/Errors/For-each-in_loops_are_deprecated +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="消息">消息</h2> + +<pre class="syntaxbox">警告: JavaScript 1.6's 版本的for-each-in遍历不再赞成使用; 建议用 ES6的 for-of 替换 +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>警告</p> + +<h2 id="出了什么问题">出了什么问题?</h2> + +<p>JavaScript 1.6's {{jsxref("Statements/for_each...in", "for each (variable in obj)")}}语法不赞成使用, 将在未来版本移除它。</p> + +<h2 id="实例">实例</h2> + +<h3 id="遍历对象">遍历对象</h3> + +<p>{{jsxref("Statements/for_each...in", "for each...in")}} 用来遍历指定对象.</p> + +<h4 id="不赞成这么使用">不赞成这么使用</h4> + +<div class="warning"> +<p class="brush: js example-bad">var object = { a: 10, b: 20 };</p> + +<p class="brush: js example-bad">for each (var x in object) {<br> + console.log(x); // 10<br> + // 20<br> + }</p> +</div> + +<h4 id="替换语法">替换语法</h4> + +<p>你可以使用 {{jsxref("Statements/for...in", "for...in")}} 遍历指定对象, 获取每次循环的值:</p> + +<pre class="brush: js example-good">var object = { a: 10, b: 20 }; + +for (var key in object) { + var x = object[key]; + console.log(x); // 10 + // 20 +} +</pre> + +<p>也可以使用{jsxref("Statements/for...of", "for...of")}} (ES2015) 和 {{jsxref("Object.values")}} (ES2017), 你可以获取指定对象的值得数组然后像这样遍历它:</p> + +<pre class="brush: js example-good">var object = { a: 10, b: 20 }; + +for (var x of Object.values(object)) { + console.log(x); // 10 + // 20 +} +</pre> + +<h3 id="数组遍历">数组遍历</h3> + +<p>{{jsxref("Statements/for_each...in", "for each...in")}} 被用于遍历制定数组.</p> + +<h4 id="不赞成这么使用_2">不赞成这么使用</h4> + +<div class="warning"> +<p class="brush: js example-bad">var array = [10, 20, 30];</p> + +<p class="brush: js example-bad">for each (var x in array) {<br> + console.log(x); // 10<br> + // 20<br> + // 30<br> + }</p> +</div> + +<h4 id="替换语法_2">替换语法</h4> + +<p>现在最好用{{jsxref("Statements/for...of", "for...of")}} (ES2015) 替换</p> + +<pre class="brush: js example-good">var array = [10, 20, 30]; + +for (var x of array) { + console.log(x); // 10 + // 20 + // 30 +} +</pre> + +<h3 id="遍历一个空数组">遍历一个空数组</h3> + +<p>{{jsxref("Statements/for_each...in", "for each...in")}} 如果指定值是 <code>null</code> o或 <code>undefined什么都遍历不出来。</code> {{jsxref("Statements/for...of", "for...of")}} 在这种情况会抛出异常.</p> + +<h4 id="不赞成这么使用_3">不赞成这么使用</h4> + +<div class="warning"> +<p class="brush: js example-bad">function func(array) {<br> + for each (var x in array) {<br> + console.log(x);<br> + }<br> + }<br> + func([10, 20]); // 10<br> + // 20<br> + func(null); // prints nothing<br> + func(undefined); // prints nothing</p> +</div> + +<h4 id="替换语法_3">替换语法</h4> + +<p>用{{jsxref("Statements/for...of", "for...of")}} 重写{{jsxref("Statements/for_each...in", "for each...in")}} 后值可以为 <code>null</code> 和 <code>undefined</code> ,同时你需要警惕{{jsxref("Statements/for...of", "for...of")}}抛出的异常.</p> + +<pre class="brush: js example-good">function func(array) { + if (array) { + for (var x of array) { + console.log(x); + } + } +} +func([10, 20]); // 10 + // 20 +func(null); // prints nothing +func(undefined); // prints nothing +</pre> + +<h3 id="遍历对象键值对">遍历对象键值对</h3> + +<h4 id="不赞成这么使用_4">不赞成这么使用</h4> + +<p> 不赞成使用{{jsxref("Statements/for_each...in", "for each...in")}}和{{jsxref("Iterator")}} 对象来遍历指定对象的键值对.</p> + +<div class="warning"> +<p class="brush: js example-bad">var object = { a: 10, b: 20 };</p> + +<p class="brush: js example-bad">for each (var [key, value] in Iterator(object)) {<br> + console.log(key, value); // "a", 10<br> + // "b", 20<br> + }</p> +</div> + +<h4 id="替换语法_4">替换语法</h4> + +<p>你可以使用 {{jsxref("Statements/for...in", "for...in")}} 遍历指定对象,获取每次循环的值:</p> + +<pre class="brush: js example-good">var object = { a: 10, b: 20 }; + +for (var key in object) { + var value = object[key]; + console.log(key, value); // "a", 10 + // "b", 20 +} +</pre> + +<p>也可以使用{jsxref("Statements/for...of", "for...of")}} (ES2015) 和 {{jsxref("Object.values")}} (ES2017), 你可以获取指定对象的值得数组然后像这样遍历它:</p> + +<pre class="brush: js example-good">var object = { a: 10, b: 20 }; + +for (var [key, value] of Object.entries(object)) { + console.log(key, value); // "a", 10 + // "b", 20 +} +</pre> + +<h2 id="参见">参见</h2> + +<ul> + <li>{{jsxref("Statements/for...of", "for...of")}}</li> + <li>{{jsxref("Object.values")}}</li> + <li>{{jsxref("Object.entries")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/getter_only/index.html b/files/zh-cn/web/javascript/reference/errors/getter_only/index.html new file mode 100644 index 0000000000..0636f30eea --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/getter_only/index.html @@ -0,0 +1,82 @@ +--- +title: 'TypeError: setting getter-only property "x"' +slug: Web/JavaScript/Reference/Errors/Getter_only +tags: + - JavaScript + - 严格模式 + - 类型错误 + - 错误提示 +translation_of: Web/JavaScript/Reference/Errors/Getter_only +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="错误提示">错误提示</h2> + +<pre class="syntaxbox">TypeError: setting getter-only property "x" (Firefox) +TypeError: Cannot set property "prop" of #<Object> which has only a getter (Chrome) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>仅在<a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">严格模式</a>下报 {{jsxref("TypeError")}} 错误。</p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p>该错误提示出现于试图给一个仅仅设置了 <a href="/en-US/docs/Web/JavaScript/Reference/Functions/get">getter</a> 特性的属性赋新值的时候。在非严格模式下会被静默忽略,而在<a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">严格模式</a>下会报 {{jsxref("TypeError")}} 错误。</p> + +<h2 id="示例">示例</h2> + +<p>下面的例子展示了如何为一个属性设置 getter 特性。由于没有设置 <a href="/en-US/docs/Web/JavaScript/Reference/Functions/set">setter</a> 特性,所以在试图将 temperature 属性值设置为 30 的时候会报 TypeError 的错误。相关细节信息可以参考 {{jsxref("Object.defineProperty()")}} 页面。</p> + +<pre class="brush: js example-bad">"use strict"; + +function Archiver() { + var temperature = null; + Object.defineProperty(this, 'temperature', { + get: function() { + console.log('get!'); + return temperature; + } + }); +} + +var arc = new Archiver(); +arc.temperature; // 'get!' + +arc.temperature = 30; +// TypeError: setting getter-only property "temperature"</pre> + +<p>至于修复问题的方法,可以将第 16 行的代码移除,因为它试图为 temperature 属性赋值,或者是为它添加一个 <a href="/en-US/docs/Web/JavaScript/Reference/Functions/set">setter</a> 特性,就像下面这样:</p> + +<pre class="brush: js example-good highlight[12]">"use strict"; + +function Archiver() { + var temperature = null; + var archive = []; + + Object.defineProperty(this, 'temperature', { + get: function() { + console.log('get!'); + return temperature; + }, + set: function(value) { + temperature = value; + archive.push({ val: temperature }); + } + }); + + this.getArchive = function() { return archive; }; +} + +var arc = new Archiver(); +arc.temperature; // 'get!' +arc.temperature = 11; +arc.temperature = 13; +arc.getArchive(); // [{ val: 11 }, { val: 13 }]</pre> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li>{{jsxref("Object.defineProperty()")}}</li> + <li>{{jsxref("Object.defineProperties()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/identifier_after_number/index.html b/files/zh-cn/web/javascript/reference/errors/identifier_after_number/index.html new file mode 100644 index 0000000000..c2607edd2b --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/identifier_after_number/index.html @@ -0,0 +1,48 @@ +--- +title: 'SyntaxError: identifier starts immediately after numeric literal' +slug: Web/JavaScript/Reference/Errors/Identifier_after_number +translation_of: Web/JavaScript/Reference/Errors/Identifier_after_number +--- +<div>{{JSSidebar("Errors")}}</div> + +<h2 id="Message">Message</h2> + +<pre class="syntaxbox">SyntaxError: identifier starts immediately after numeric literal (Firefox) +SyntaxError: Unexpected number (Chrome) +</pre> + +<h2 id="Error_type">Error type</h2> + +<p>{{jsxref("SyntaxError")}}</p> + +<h2 id="What_went_wrong">What went wrong?</h2> + +<p>变量名叫{{Glossary("Identifier", "identifiers")}},它符合某些规则,而你打破了这些规则!</p> + +<p>一个JavaScript标识符必须以字母开头,下划线(_)或美元符号($)。他们不能以数字开头。只有后续字符可以是数字(0-9)。</p> + +<h2 id="Examples">Examples</h2> + +<h3 id="Variable_names_starting_with_numeric_literals">Variable names starting with numeric literals</h3> + +<p>Variable names can't start with numbers in JavaScript. The following fails:</p> + +<pre class="brush: js example-bad">var 1life = 'foo'; +// SyntaxError: identifier starts immediately after numeric literal + +var foo = 1life; +// SyntaxError: identifier starts immediately after numeric literal +</pre> + +<p>You will need to rename your variable to avoid the leading number.</p> + +<pre class="brush: js example-good">var life1 = 'foo'; +var foo = life1; +</pre> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Lexical_grammar">Lexical grammar</a></li> + <li><a href="/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#Variables">Variables</a> in the <a href="/en-US/docs/Web/JavaScript/Guide">JavaScript Guide</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/illegal_character/index.html b/files/zh-cn/web/javascript/reference/errors/illegal_character/index.html new file mode 100644 index 0000000000..16367c43a2 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/illegal_character/index.html @@ -0,0 +1,73 @@ +--- +title: 'SyntaxError: illegal character' +slug: Web/JavaScript/Reference/Errors/Illegal_character +tags: + - JavaScript + - 语法错误 +translation_of: Web/JavaScript/Reference/Errors/Illegal_character +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="错误提示">错误提示</h2> + +<pre class="syntaxbox">SyntaxError: illegal character (Firefox) +SyntaxError: Invalid or unexpected token (Chrome) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("SyntaxError")}}</p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p>在代码中有非法的或者不期望出现的标记符号出现在不该出现的位置。请使用支持语法高亮功能的编辑器仔细检查你的代码,看看是否存在张冠李戴的情况,比如减号 (<code> - </code>) 与连接符 (<code> – </code>) ,或者是英文双引号 (<code> " </code>) 与中文双引号 (<code> “ </code>)。</p> + +<h2 id="示例">示例</h2> + +<h3 id="错配字符">错配字符</h3> + +<p>一些字符看起来会很相像,但是会导致于语法解析器解析代码失败。</p> + +<pre class="brush: js example-bad">“This looks like a string”; +// SyntaxError: illegal character + +42 – 13; +// SyntaxError: illegal character +</pre> + +<p>下面这样是可以正常运行的:</p> + +<pre class="brush: js example-good">"This is actually a string"; + +42 - 13; +</pre> + +<h3 id="遗漏的字符">遗漏的字符</h3> + +<p>很容易就会在这里或那里遗漏一些字符。</p> + +<pre class="brush: js example-bad">var colors = ['#000', #333', '#666']; +// SyntaxError: illegal character +</pre> + +<p>把遗漏的引号给 '#333' 添加上。</p> + +<pre class="brush: js example-good">var colors = ['#000', '#333', '#666'];</pre> + +<h3 id="隐藏字符">隐藏字符</h3> + +<p>当从外部复制粘贴代码的时候,有可能就有非法的隐藏字符的存在,需要引起注意!</p> + +<pre class="brush: js example-bad">var foo = 'bar'; +// SyntaxError: illegal character +</pre> + +<p>当使用文本编辑器如VIM进行探测的时候,可以发现这里存在一个零宽空格<a href="https://en.wikipedia.org/wiki/Zero-width_space"> (ZWSP) (U+200B)</a> 。</p> + +<pre class="brush: js">var foo = 'bar';<200b></pre> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Lexical_grammar">Lexical grammar</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/in_operator_no_object/index.html b/files/zh-cn/web/javascript/reference/errors/in_operator_no_object/index.html new file mode 100644 index 0000000000..fa607780a7 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/in_operator_no_object/index.html @@ -0,0 +1,71 @@ +--- +title: 'TypeError: invalid ''in'' operand "x"' +slug: Web/JavaScript/Reference/Errors/in_operator_no_object +tags: + - JavaScript + - 类型错误 + - 错误提示 +translation_of: Web/JavaScript/Reference/Errors/in_operator_no_object +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="错误提示">错误提示</h2> + +<pre class="syntaxbox">TypeError: invalid 'in' operand "x" (Firefox) +TypeError: Cannot use 'in' operator to search for 'x' in y (Chrome) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("TypeError")}}</p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p><a href="/en-US/docs/Web/JavaScript/Reference/Operators/in"><code>in</code> 操作符</a>只可以用来检测对象中是否存在某个属性,而不能用来在字符串、数字或者其他基本类型的数据中进行检索。</p> + +<h2 id="示例">示例</h2> + +<h3 id="在字符串中进行检索">在字符串中进行检索</h3> + +<p>与其他语言不同(如 Python),不能使用 <a href="/en-US/docs/Web/JavaScript/Reference/Operators/in"><code>in</code> 操作符</a>在字符串中进行检索。</p> + +<pre class="brush: js example-bad">"Hello" in "Hello World"; +// TypeError: invalid 'in' operand "Hello World" +</pre> + +<p>可以使用 {{jsxref("String.prototype.indexOf()")}} 来代替:</p> + +<pre class="brush: js example-good">"Hello World".indexOf("Hello") !== -1; +// true</pre> + +<h3 id="操作数不能为_null_或者_undefined">操作数不能为 null 或者 undefined</h3> + +<p>确保你将要进行探测的对象不为 {{jsxref("null")}} 或者 {{jsxref("undefined")}}.</p> + +<pre class="brush: js example-bad">var foo = null; +"bar" in foo; +// TypeError: invalid 'in' operand "foo" +</pre> + +<p>in 操作符的预期操作数只有对象类型。</p> + +<pre class="brush: js example-good">var foo = { baz: "bar" }; +"bar" in foo; // false + +"PI" in Math; // true +"pi" in Math; // false +</pre> + +<h3 id="在数组中进行检索">在数组中进行检索</h3> + +<p>当使用 in 操作符来对 {{jsxref("Array")}} 对象进行检索的时候一定要特别小心,因为它检测的是索引值而不是位于索引位置的值。</p> + +<pre class="brush: js">var trees = ['redwood', 'bay', 'cedar', 'oak', 'maple']; +3 in trees; // true +"oak" in trees; // false</pre> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/in"><code>in</code> operator</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/index.html b/files/zh-cn/web/javascript/reference/errors/index.html new file mode 100644 index 0000000000..f0c0f2560a --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/index.html @@ -0,0 +1,28 @@ +--- +title: JavaScript 错误参考 +slug: Web/JavaScript/Reference/Errors +tags: + - Debugging + - Errors + - JavaScript + - 调试 + - 错误 +translation_of: Web/JavaScript/Reference/Errors +--- +<p>{{jsSidebar("Errors")}}</p> + +<p>下面列出了 JavaScript 抛出的错误。这些错误是有用的调试帮助,但报告的问题并不总是十分明了。下面的页面提供有关这些错误的详细信息。每个错误都是基于 {{jsxref("Error")}} 的对象,并且具有名称和消息。</p> + +<p>Web 控制台中显示的错误可能包含指向下面相应页面的链接,以帮助您快速理解代码中的问题。</p> + +<h2 id="错误列表">错误列表</h2> + +<p>在此列表中,每个页面按名称(错误类型)和信息(更详细的容易理解的错误信息)列出。 总之,这两者提供了理解和解决错误的线索。 有关更多信息,请按照以下链接!</p> + +<p>{{ListSubPages("/zh-CN/docs/Web/JavaScript/Reference/Errors")}}</p> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li><a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/What_went_wrong">What went wrong? Troubleshooting JavaScript</a>: Beginner's introductory tutorial on fixing JavaScript errors.</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/invalid_array_length/index.html b/files/zh-cn/web/javascript/reference/errors/invalid_array_length/index.html new file mode 100644 index 0000000000..6e74db20b8 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/invalid_array_length/index.html @@ -0,0 +1,77 @@ +--- +title: 'RangeError: invalid array length' +slug: Web/JavaScript/Reference/Errors/Invalid_array_length +tags: + - JavaScript + - 范围错误 + - 错误 +translation_of: Web/JavaScript/Reference/Errors/Invalid_array_length +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="错误信息">错误信息</h2> + +<pre class="syntaxbox">RangeError: invalid array length (Firefox) +RangeError: Invalid array length (Chrome) +RangeError: Invalid array buffer length (Chrome) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("RangeError")}}</p> + +<h2 id="什么地方出错了">什么地方出错了?</h2> + +<p>无效的数组错误长度通常会在以下情形中出现:</p> + +<ul> + <li>当创建一个长度为负数或者长度大于等于2<sup>32 </sup>的<sup> </sup>{{jsxref("Array")}} 或者 {{jsxref("ArrayBuffer")}} 时。</li> + <li>当设置 {{jsxref("Array.length")}} 属性为负数或者长度大于等于2<sup>32 </sup>时。</li> +</ul> + +<p>为什么 Array(数组) 和 ArrayBuffer(数组缓冲区) 的长度会受到限制?因为 Array 和 ArrayBuffer 的 length(长度) 属性被定义为一个32位无符号整形(unsigned 32-bit integer)的值,所以它只能存储 0 - 2<sup>32</sup>-1 之间的数。</p> + +<p>当你使用构造函数来创建一个数组的时候,你可能想使用字面值的形式,第一个参数会被解释为数组的长度。</p> + +<p>或者说,你想要在设置数组之前确定它的长度,或把它作为一个构造函数的参数。</p> + +<h2 id="示例">示例</h2> + +<h3 id="错误的示例">错误的示例</h3> + +<pre class="brush: js example-bad">new Array(Math.pow(2, 40)) +new Array(-1) +new ArrayBuffer(Math.pow(2, 32)) +new ArrayBuffer(-1) + +let a = []; +a.length = a.length - 1; // 将 length 属性的值设置为 -1 + +let b = new Array(Math.pow(2, 32) - 1); +b.length = b.length + 1; // 将 length 属性的值设置为 2^32 +</pre> + +<h3 id="正确的示例">正确的示例</h3> + +<pre class="brush: js example-good">[ Math.pow(2, 40) ] // [ 1099511627776 ] +[ -1 ] // [ -1 ] +new ArrayBuffer(Math.pow(2, 32) - 1) +new ArrayBuffer(0) + +let a = []; +a.length = Math.max(0, a.length - 1); + +let b = new Array(Math.pow(2, 32) - 1); +b.length = Math.min(0xffffffff, b.length + 1); + +// 0xffffffff 是 2^32 - 1 的 十六进制 表示方式 +// 它也可以被写作 (-1 >>> 0) +</pre> + +<h2 id="相关页面">相关页面</h2> + +<ul> + <li>{{jsxref("Array")}}</li> + <li>{{jsxref("Array.length")}}</li> + <li>{{jsxref("ArrayBuffer")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/invalid_assignment_left-hand_side/index.html b/files/zh-cn/web/javascript/reference/errors/invalid_assignment_left-hand_side/index.html new file mode 100644 index 0000000000..e3506fc199 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/invalid_assignment_left-hand_side/index.html @@ -0,0 +1,54 @@ +--- +title: 'ReferenceError: invalid assignment left-hand side' +slug: Web/JavaScript/Reference/Errors/Invalid_assignment_left-hand_side +tags: + - Errors + - JavaScript + - ReferenceError +translation_of: Web/JavaScript/Reference/Errors/Invalid_assignment_left-hand_side +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="Message">Message</h2> + +<pre class="syntaxbox">ReferenceError: invalid assignment left-hand side +</pre> + +<h2 id="Error_type">Error type</h2> + +<p>{{jsxref("ReferenceError")}}.</p> + +<h2 id="What_went_wrong">What went wrong?</h2> + +<p>有时会出现不可预料的赋值情况。这可能是因为<a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/Assignment_Operators">赋值运算符</a>或<a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/Comparison_Operators">比较运算符</a>不匹配的缘故。正确的是,使用“=”号将值赋给一个变量,使用“==”或者“===”来比较一个值。</p> + +<h2 id="Examples">Examples</h2> + +<pre class="brush: js example-bad">if (Math.PI = 3 || Math.PI = 4) { + console.log('no way!'); +} +// ReferenceError: invalid assignment left-hand side + +var str = 'Hello, ' ++= 'is it me ' ++= 'you\'re looking for?'; +// ReferenceError: invalid assignment left-hand side +</pre> + +<p>在 <code>if</code> 语句中,你要使用比较运算符("=="),而在字符串连接中,使用加号运算符("+")。</p> + +<pre class="brush: js example-good">if (Math.PI == 3 || Math.PI == 4) { + console.log('no way!'); +} + +var str = 'Hello, ' ++ 'from the ' ++ 'other side!'; +</pre> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/Assignment_Operators">赋值运算符</a></li> + <li><a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/Comparison_Operators">比较运算符</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/invalid_const_assignment/index.html b/files/zh-cn/web/javascript/reference/errors/invalid_const_assignment/index.html new file mode 100644 index 0000000000..330bab78c8 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/invalid_const_assignment/index.html @@ -0,0 +1,90 @@ +--- +title: 'TypeError: invalid assignment to const "x"' +slug: Web/JavaScript/Reference/Errors/Invalid_const_assignment +tags: + - JavaScript + - 类型错误 + - 错误提示 +translation_of: Web/JavaScript/Reference/Errors/Invalid_const_assignment +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="错误提示">错误提示</h2> + +<pre class="syntaxbox">TypeError: invalid assignment to const "x" (Firefox) +TypeError: Assignment to constant variable. (Chrome) +TypeError: Redeclaration of const 'x' (IE/Edge) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("TypeError")}}</p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p>常量指的是无法在程序正常运行过程中进行修改的值。一方面无法通过重新赋值进行修改,另外一方面也无法进行重新声明。在 JavaScript 中,常量通过关键字 <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/const">const</a></code> 来声明。</p> + +<h2 id="示例">示例</h2> + +<h3 id="不合法的二次声明">不合法的二次声明</h3> + +<p>在同一作用域内为相同的常量名进行赋值会报错。</p> + +<pre class="brush: js example-bad">const COLUMNS = 80; + +// ... + +COLUMNS = 120; // TypeError: invalid assignment to const `COLUMNS'</pre> + +<h3 id="问题修复">问题修复</h3> + +<p>修复的方式有很多种。可以根据你想要达到的目的来有针对性地对其进行处理。</p> + +<h4 id="重新命名">重新命名</h4> + +<p>如果想要声明另一个变量,那么请选择其他名称对其重新命名。原来的常量名在该作用域中已经被占有。</p> + +<pre class="brush: js example-good">const COLUMNS = 80; +const WIDE_COLUMNS = 120;</pre> + +<h4 id="const_let_or_var"><code>const</code>, <code>let</code> or <code>var</code>?</h4> + +<p>如果你的目的不是为了创建一个常量的话,那么就不要使用 const 关键字。可以使用 <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/let">let</a></code> 关键字来声明一个拥有块作用域的变量,或者使用 <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/var">var</a> </code>来声明一个全局变量。</p> + +<pre class="brush: js example-good">let columns = 80; + +// ... + +let columns = 120; +</pre> + +<h4 id="作用域">作用域</h4> + +<p>检查一下作用域是否正确。例如这个常量是否应该出现在当前作用域,还是应该出现在函数内部?</p> + +<pre class="brush: js example-good">const COLUMNS = 80; + +function setupBigScreenEnvironment() { + const COLUMNS = 120; +}</pre> + +<h3 id="const与不可变性"><code>const与不可变性</code></h3> + +<p>const 声明语句创建了一个对值的只读引用。这并<strong>不</strong>意味着它指向的值是不可变的,而是说这个变量标记符不能被再次分配。例如,在值是对象的情况下,引用的对象自身内容依然是可以改变的。不能改变该变量的引用:</p> + +<pre class="brush: js example-bad">const obj = {foo: 'bar'}; +obj = {foo: 'baz'}; // TypeError: invalid assignment to const `obj' +</pre> + +<p>但是可以改变它引用的值的属性: </p> + +<pre class="brush: js example-good">obj.foo = 'baz'; +obj; // Object { foo: "baz" }</pre> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/const">const</a></code></li> + <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/let">let</a></code></li> + <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/var">var</a></code></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/invalid_date/index.html b/files/zh-cn/web/javascript/reference/errors/invalid_date/index.html new file mode 100644 index 0000000000..1deee46fcc --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/invalid_date/index.html @@ -0,0 +1,54 @@ +--- +title: 'RangeError: invalid date' +slug: Web/JavaScript/Reference/Errors/Invalid_date +tags: + - JavaScript + - 范围错误 + - 错误 +translation_of: Web/JavaScript/Reference/Errors/Invalid_date +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="消息">消息</h2> + +<pre class="syntaxbox">范围错误: 非法数据(Firefox) +范围错误: 非法时间值 (Chrome) +范围错误: 提供的数据不是有效的 (Chrome) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("RangeError")}}</p> + +<h2 id="哪里出错了">哪里出错了?</h2> + +<p>为 {{jsxref("Date")}} 或 {{jsxref("Date.parse()")}} 提供了一个会导致无效日期的字符串。</p> + +<h2 id="示例">示例</h2> + +<h3 id="错误示例">错误示例</h3> + +<p>ISO格式化字符串中不可识别的字符串或者包含非法元素值的日期一般会返回 {{jsxref("NaN")}}。然而,根据实现的不同,不符合ISO格式的字符串可能也会抛出<code> RangeError: invalid date</code>,比如在火狐浏览器中有以下情形:</p> + +<pre class="brush: js example-bad">new Date('foo-bar 2014'); +new Date('2014-25-23').toISOString(); +new Date('foo-bar 2014').toString(); +</pre> + +<p>然而下面这种情形会返回 {{jsxref("NaN")}} :</p> + +<pre class="brush: js example-bad">Date.parse('foo-bar 2014'); // NaN</pre> + +<p>参见 {{jsxref("Date.parse()")}} 文档,了解更多详情。</p> + +<h3 id="正确示例">正确示例</h3> + +<pre class="brush: js example-good">new Date('05 October 2011 14:48 UTC');</pre> + +<h2 id="另见">另见</h2> + +<ul> + <li>{{jsxref("Date")}}</li> + <li>{{jsxref("Date.prototype.parse()")}}</li> + <li>{{jsxref("Date.prototype.toISOString()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/invalid_for-in_initializer/index.html b/files/zh-cn/web/javascript/reference/errors/invalid_for-in_initializer/index.html new file mode 100644 index 0000000000..34dcefa8ed --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/invalid_for-in_initializer/index.html @@ -0,0 +1,73 @@ +--- +title: 'SyntaxError: for-in loop head declarations may not have initializers' +slug: Web/JavaScript/Reference/Errors/Invalid_for-in_initializer +tags: + - JavaScript + - 严格模式 + - 语法错误 +translation_of: Web/JavaScript/Reference/Errors/Invalid_for-in_initializer +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="错误提示">错误提示</h2> + +<pre class="syntaxbox">SyntaxError: for-in loop head declarations may not have initializers (Firefox) + +SyntaxError: for-in loop variable declaration may not have an initializer. (Chrome) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>该 {{jsxref("SyntaxError")}} 只出现于<a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">严格模式</a>下。</p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p>在 <a href="/en-US/docs/Web/JavaScript/Reference/Statements/for...in">for...in</a> 循环的头部存在初始化表达式。 也就是存在变量声明并且被赋值,例如 |<code>for (var i = 0 in obj)</code>|。在非严格模式下,这种在循环头部的变量声明会被静默忽略,语句的表现形式与 <code>|for (var i in obj)|</code>相同。而在<a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">严格模式</a>下,会报语法错误。</p> + +<h2 id="示例">示例</h2> + +<p>下面这个示例会报语法错误(<code>SyntaxError</code>):</p> + +<pre class="brush: js example-bad">"use strict"; + +var obj = {a: 1, b: 2, c: 3 }; + +for (var i = 0 in obj) { + console.log(obj[i]); +} + +// SyntaxError: for-in loop head declarations may not have initializers +</pre> + +<h3 id="合法的_for-in_循环">合法的 for-in 循环</h3> + +<p>可以把初始化语句 (<code>i = 0</code>) 从 for-in 循环的头部移除。</p> + +<pre class="brush: js example-good">"use strict"; + +var obj = {a: 1, b: 2, c: 3 }; + +for (var i in obj) { + console.log(obj[i]); +} +</pre> + +<h3 id="数组迭代">数组迭代</h3> + +<p>for...in 循环<a href="/en-US/docs/Web/JavaScript/Reference/Statements/for...in#Array_iteration_and_for...in">不应该应用于数组迭代中</a>。是否考虑使用 <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/for">for</a></code> 循环而不是 <code>for-in</code> 循环来遍历数组({{jsxref("Array")}})?在 for 循环中是允许使用初始化语句的:</p> + +<pre class="brush: js example-good">var arr = [ "a", "b", "c" ] + +for (var i = 2; i < arr.length; i++) { + console.log(arr[i]); +} + +// "c"</pre> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/for...in">for...in</a></code></li> + <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/for...of">for...of</a></code> – 无论是在严格模式下还是非严格模式下也都不允许使用初始化语句。</li> + <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/for">for</a></code> – 更适用于数组迭代,因为允许使用初始化语句。</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/invalid_for-of_initializer/index.html b/files/zh-cn/web/javascript/reference/errors/invalid_for-of_initializer/index.html new file mode 100644 index 0000000000..7e93d3af35 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/invalid_for-of_initializer/index.html @@ -0,0 +1,63 @@ +--- +title: >- + SyntaxError: a declaration in the head of a for-of loop can't have an + initializer +slug: Web/JavaScript/Reference/Errors/Invalid_for-of_initializer +tags: + - JavaScript + - 语法错误 + - 错误提示 +translation_of: Web/JavaScript/Reference/Errors/Invalid_for-of_initializer +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="错误信息">错误信息</h2> + +<pre class="syntaxbox">SyntaxError: a declaration in the head of a for-of loop can't have an initializer (Firefox) + +SyntaxError: for-of loop variable declaration may not have an initializer. (Chrome) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("SyntaxError")}}</p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p><a href="/en-US/docs/Web/JavaScript/Reference/Statements/for...of">for...of</a> 循环的头部包含有初始化表达式。也就是对一个变量进行声明并赋值|<code>for (var i = 0 of iterable)</code>|。这在 for-of 循环中是被禁止的。你想要的可能是允许包含初始化器的 <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/for">for</a></code> 循环形式。</p> + +<h2 id="示例">示例</h2> + +<h3 id="非法的_for-of_循环形式">非法的 <code>for-of</code> 循环形式</h3> + +<pre class="brush: js example-bad">let iterable = [10, 20, 30]; + +for (let value = 50 of iterable) { + console.log(value); +} + +// SyntaxError: a declaration in the head of a for-of loop can't +// have an initializer</pre> + +<h3 id="合法的_for-of_循环形式">合法的 <code>for-of</code> 循环形式</h3> + +<p>需要将初始化器 (<code>value = 50</code>) 从<code>for-of</code> 循环的头部移除。或许你的本意是给每个值添加 50 的偏移量,在这种情况下,可以在循环体中进行添加。</p> + +<pre class="brush: js example-good">let iterable = [10, 20, 30]; + +for (let value of iterable) { + value += 50; + console.log(value); +} +// 60 +// 70 +// 80 +</pre> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/for...of">for...of</a></code></li> + <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/for...in">for...in</a></code> – 在严格模式下也同样禁止使用初始化器 (<a href="/en-US/docs/Web/JavaScript/Reference/Errors/Invalid_for-in_initializer">SyntaxError: for-in loop head declarations may not have initializers</a>)</li> + <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/for">for</a></code> – 在迭代时允许定义初始化器</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/invalid_right_hand_side_instanceof_operand/index.html b/files/zh-cn/web/javascript/reference/errors/invalid_right_hand_side_instanceof_operand/index.html new file mode 100644 index 0000000000..2ce2214034 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/invalid_right_hand_side_instanceof_operand/index.html @@ -0,0 +1,56 @@ +--- +title: 'TypeError: invalid ''instanceof'' operand ''x''' +slug: Web/JavaScript/Reference/Errors/invalid_right_hand_side_instanceof_operand +translation_of: Web/JavaScript/Reference/Errors/invalid_right_hand_side_instanceof_operand +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="消息">消息</h2> + +<pre class="syntaxbox">TypeError: invalid 'instanceof' operand "x" (Firefox) +TypeError: "x" is not a function (Firefox) +TypeError: Right-hand side of 'instanceof' is not an object (Chrome) +TypeError: Right-hand side of 'instanceof' is not callable (Chrome)</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("TypeError")}}</p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p><a href="/en-US/docs/Web/JavaScript/Reference/Operators/instanceof"><code>instanceof</code> 操作符</a> 希望右边的操作数为一个构造对象,即一个有 <code>prototype</code> 属性且可以调用的对象。</p> + +<h2 id="例子">例子</h2> + +<pre class="brush: js example-bad">"test" instanceof ""; // TypeError: invalid 'instanceof' operand "" +42 instanceof 0; // TypeError: invalid 'instanceof' operand 0 + +function Foo() {} +var f = Foo(); // Foo() is called and returns undefined +var x = new Foo(); + +x instanceof f; // TypeError: invalid 'instanceof' operand f +x instanceof x; // TypeError: x is not a function +</pre> + +<p>为了解决上述问题, 你可能需要将<a href="/en-US/docs/Web/JavaScript/Reference/Operators/instanceof"><code>instanceof</code> 操作符</a> 换成 <a href="/en-US/docs/Web/JavaScript/Reference/Operators/typeof"><code>typeof</code> 操作符</a>, 或者确保你使用的是函数名称,而不是函数计算的结果。</p> + +<pre class="brush: js example-good">typeof "test" == "string"; // true +typeof 42 == "number" // true + +function Foo() {} +var f = Foo; // Do not call Foo. +var x = new Foo(); + +x instanceof f; // true +x instanceof Foo; // true +</pre> + +<h2 id="参见">参见</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/instanceof"><code>instanceof</code> 操作符</a></li> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/typeof"><code>typeof</code> 操作符</a></li> +</ul> + +<p> </p> diff --git a/files/zh-cn/web/javascript/reference/errors/is_not_iterable/index.html b/files/zh-cn/web/javascript/reference/errors/is_not_iterable/index.html new file mode 100644 index 0000000000..a3cd34eb82 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/is_not_iterable/index.html @@ -0,0 +1,126 @@ +--- +title: 'TypeError: ''x'' is not iterable' +slug: Web/JavaScript/Reference/Errors/is_not_iterable +tags: + - 生成器 + - 迭代器 +translation_of: Web/JavaScript/Reference/Errors/is_not_iterable +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="错误信息">错误信息</h2> + +<pre class="syntaxbox">TypeError: 'x' is not iterable (Firefox, Chrome) +TypeError: 'x' is not a function or its return value is not iterable (Chrome) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("TypeError")}}</p> + +<h2 id="发生了什么错误">发生了什么错误?</h2> + +<p>这个值作为 <a href="/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration#for...of_statement">for…of </a>的表达式右值,或者作为一个函数的参数,如 {{jsxref("Promise.all")}} 或者 {{jsxref("TypedArray.from")}}, 不是一个 <a href="/en-US/docs/Web/JavaScript/Reference/Iteration_protocols">可迭代对象</a>. 一个可迭代对象可以是一个内置可迭代类型,如{{jsxref("Array")}}, {{jsxref("String")}} 或 {{jsxref("Map")}}, 一个 generator 生成结果, 或者一个实现了 <a href="/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol">可迭代协议</a> 的对象</p> + +<h2 id="示例">示例</h2> + +<h3 id="Iterating_over_Object_properties">Iterating over Object properties</h3> + +<p>在JavaScript中, {{jsxref("Object")}} 是不可迭代的,除非它们实现了<a href="/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol">迭代协议</a>. 因此, 你不能使用 <a href="/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration#for...of_statement">for…of </a>来迭代对象的属性.</p> + +<pre class="brush: js example-bad">var obj = { 'France': 'Paris', 'England': 'London' }; +for (let p of obj) { // TypeError: obj is not iterable + // … +} +</pre> + +<p>做为替代你必须使用 {{jsxref("Object.keys")}} 或 {{jsxref("Object.entries")}} 来迭代对象的属性或属性值.</p> + +<pre class="brush: js example-good">var obj = { 'France': 'Paris', 'England': 'London' }; +// 迭代属性名称: +for (let country of <code>Object</code>.keys(obj)) { + var capital = obj[country]; + console.log(country, capital); +} + +for (const [country, capital] of <code>Object</code>.entries(obj)) + console.log(country, capital); +</pre> + +<p>这次case的另外一个选择是使用 {{jsxref("Map")}}:</p> + +<pre class="brush: js example-good">var map = new Map; +map.set('France', 'Paris'); +map.set('England', 'London'); +// Iterate over the property names: +for (let country of map.keys()) { + let capital = map[country]; + console.log(country, capital); +} + +for (let capital of map.values()) + console.log(capital); + +for (const [country, capital] of map.entries()) + console.log(country, capital); +</pre> + +<h3 id="Iterating_over_a_generator">Iterating over a generator</h3> + +<p><a href="/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators#Generators">Generators</a> 是用来生成可迭代对象的函数.</p> + +<pre class="brush: js example-bad">function* generate(a, b) { + yield a; + yield b; +} + +for (let x of generate) // TypeError: generate is not iterable + console.log(x); +</pre> + +<p>当它没有被调用, 这个 {{jsxref("Function")}} 相应的是可调用的,但是不可迭代。 调用 generator 生成一个可迭代对象,该对象将迭代在生成器执行期间生成的值.</p> + +<pre class="brush: js example-good">function* generate(a, b) { + yield a; + yield b; +} + +for (let x of generate(1,2)) + console.log(x);</pre> + +<h3 id="Iterating_over_a_custom_iterable">Iterating over a custom iterable</h3> + +<p>可以使用{{jsxref("Symbol.iterator")}} 方法去实现一个自定义迭代器。你必须确定自定义的迭代器方法返回一个迭代器对象,即它必须有一个next()</p> + +<pre class="brush: js example-bad">const myEmptyIterable = { + [Symbol.iterator]() { + return [] // [] is iterable, but it is not an iterator -- it has no next method. + } +} + +Array.from(myEmptyIterable); // TypeError: myEmptyIterable is not iterable +</pre> + +<pre></pre> + +<p>下面是正确用法</p> + +<pre class="brush: js example-good">const myEmptyIterable = { + [Symbol.iterator]() { + return [][Symbol.iterator]() + } +} + +Array.from(myEmptyIterable); // [] +</pre> + +<h2 id="参阅">参阅</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol">iterable protocol</a></li> + <li>{{jsxref("Object.keys")}}</li> + <li>{{jsxref("Object.entries")}}</li> + <li>{{jsxref("Map")}}</li> + <li><a href="/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators#Generators">generators</a></li> + <li><a href="/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration#for...of_statement">for…of</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/json_bad_parse/index.html b/files/zh-cn/web/javascript/reference/errors/json_bad_parse/index.html new file mode 100644 index 0000000000..cac9176d80 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/json_bad_parse/index.html @@ -0,0 +1,111 @@ +--- +title: 'SyntaxError: JSON.parse: bad parsing' +slug: Web/JavaScript/Reference/Errors/JSON_bad_parse +tags: + - Errors + - JSON + - JavaScript + - SyntaxError + - 方法 +translation_of: Web/JavaScript/Reference/Errors/JSON_bad_parse +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="信息">信息</h2> + +<pre class="syntaxbox">SyntaxError: JSON.parse: unterminated string literal +SyntaxError: JSON.parse: bad control character in string literal +SyntaxError: JSON.parse: bad character in string literal +SyntaxError: JSON.parse: bad Unicode escape +SyntaxError: JSON.parse: bad escape character +SyntaxError: JSON.parse: unterminated string +SyntaxError: JSON.parse: no number after minus sign +SyntaxError: JSON.parse: unexpected non-digit +SyntaxError: JSON.parse: missing digits after decimal point +SyntaxError: JSON.parse: unterminated fractional number +SyntaxError: JSON.parse: missing digits after exponent indicator +SyntaxError: JSON.parse: missing digits after exponent sign +SyntaxError: JSON.parse: exponent part is missing a number +SyntaxError: JSON.parse: unexpected end of data +SyntaxError: JSON.parse: unexpected keyword +SyntaxError: JSON.parse: unexpected character +SyntaxError: JSON.parse: end of data while reading object contents +SyntaxError: JSON.parse: expected property name or '}' +SyntaxError: JSON.parse: end of data when ',' or ']' was expected +SyntaxError: JSON.parse: expected ',' or ']' after array element +SyntaxError: JSON.parse: end of data when property name was expected +SyntaxError: JSON.parse: expected double-quoted property name +SyntaxError: JSON.parse: end of data after property name when ':' was expected +SyntaxError: JSON.parse: expected ':' after property name in object +SyntaxError: JSON.parse: end of data after property value in object +SyntaxError: JSON.parse: expected ',' or '}' after property value in object +SyntaxError: JSON.parse: expected ',' or '}' after property-value pair in object literal +SyntaxError: JSON.parse: property names must be double-quoted strings +SyntaxError: JSON.parse: expected property name or '}' +SyntaxError: JSON.parse: unexpected character +SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("SyntaxError")}}</p> + +<h2 id="哪里出错了">哪里出错了?</h2> + +<p>{{jsxref("JSON.parse()")}} 会把一个字符串解析成 JSON 对象。如果字符串书写正确,那么其将会被解析成一个有效的 JSON,但是这个字符串被检测出错误语法的时候将会抛出错误。</p> + +<h2 id="示例">示例</h2> + +<h3 id="JSON.parse()_不允许在末尾添加多余的逗号"><code>JSON.parse()</code> 不允许在末尾添加多余的逗号</h3> + +<p>下面两行代码都会抛出错误:</p> + +<pre class="brush: js example-bad">JSON.parse('[1, 2, 3, 4, ]'); +JSON.parse('{"foo" : 1, }'); +// SyntaxError JSON.parse: unexpected character +// at line 1 column 14 of the JSON data +</pre> + +<p>省略末尾多余的逗号解析 JSON 就是正确:</p> + +<pre class="brush: js example-good">JSON.parse('[1, 2, 3, 4 ]'); +JSON.parse('{"foo" : 1 }');</pre> + +<h3 id="JSON_的属性名必须使用双引号">JSON 的属性名必须使用双引号</h3> + +<p>属性名上不能使用单引号,例如: 'foo'。</p> + +<pre class="brush: js example-bad">JSON.parse("{'foo' : 1 }"); +// SyntaxError: JSON.parse: expected property name or '}' +// at line 1 column 2 of the JSON data</pre> + +<p>取而代之,写成 "foo":</p> + +<pre class="brush: js example-good">JSON.parse('{"foo" : 1 }');</pre> + +<h3 id="前导_0_和小数点">前导 0 和小数点</h3> + +<p>数字不能用 0 开头,比如01,并且你的小数点后面必须跟着至少一个数字。</p> + +<pre class="brush: js example-bad">JSON.parse('{"foo" : 01 }'); +// SyntaxError: JSON.parse: expected ',' or '}' after property value +// in object at line 1 column 2 of the JSON data + +JSON.parse('{"foo" : 1. }'); +// SyntaxError: JSON.parse: unterminated fractional number +// at line 1 column 2 of the JSON data +</pre> + +<p>正确的写法应该是只写一个1,不书写前面的0。在小数点的后面至少要跟上一个数字:</p> + +<pre class="brush: js example-good">JSON.parse('{"foo" : 1 }'); +JSON.parse('{"foo" : 1.0 }'); +</pre> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{jsxref("JSON")}}</li> + <li>{{jsxref("JSON.parse()")}}</li> + <li>{{jsxref("JSON.stringify()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/malformed_formal_parameter/index.html b/files/zh-cn/web/javascript/reference/errors/malformed_formal_parameter/index.html new file mode 100644 index 0000000000..edbee71c60 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/malformed_formal_parameter/index.html @@ -0,0 +1,61 @@ +--- +title: 'SyntaxError: Malformed formal parameter' +slug: Web/JavaScript/Reference/Errors/Malformed_formal_parameter +tags: + - Errors + - JavaScript + - SyntaxError +translation_of: Web/JavaScript/Reference/Errors/Malformed_formal_parameter +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="信息">信息</h2> + +<pre class="syntaxbox">SyntaxError: malformed formal parameter (Firefox) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("SyntaxError")}}</p> + +<h2 id="哪里错了">哪里错了?</h2> + +<p>在至少带有两个参数的 <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function">Function()</a></code> 构造器中它的最后一个参数是创建的新函数的源代码。剩下的都是新函数的参数。</p> + +<p>构造器的参数有些情况下是无效的。 也许是你不小心用了一个关键字 <code>if </code>或 <code>var </code>作为参数名称,或者在参数列表中有一些杂乱的标点符号。 或者也许你不小心传递了一个无效的值,比如数字或对象。</p> + +<h2 id="好吧,这解决了我的问题。但是为什么不开始就说明白呢?">好吧,这解决了我的问题。但是为什么不开始就说明白呢?</h2> + +<p>诚然,错误信息中的措辞稍微有些奇怪。"Formal parameter" 是 "function argument" 的另一种优美的同义。 我们使用 “malformed”(即畸形)这个词,因为所有的Firefox工程师都是19世纪哥特式恐怖小说的巨星。</p> + +<h2 id="示例">示例</h2> + +<h3 id="无效的">无效的</h3> + +<pre class="brush: js example-bad">var f = Function("x y", "return x + y;"); +// SyntaxError (missing a comma) + +var f = Function("x,", "return x;"); +// SyntaxError (extraneous comma) + +var f = Function(37, "alert('OK')"); +// SyntaxError (numbers can't be argument names) +</pre> + +<h3 id="有效的">有效的</h3> + +<pre class="brush: js example-good">var f = Function("x, y", "return x + y;"); // correctly punctuated + +var f = Function("x", "return x;"); + +// if you can, avoid using Function - this is much faster +var f = function (x) { return x; }; +</pre> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function">Function()</a></code></li> + <li><a href="/en-US/docs/Web/JavaScript/Guide/Functions">About functions</a></li> + <li><a href="https://www.gutenberg.org/ebooks/84"><em>Frankenstein</em> by Mary Wollstonecraft Shelley, full e-text</a> ("Cursed (although I curse myself) be the hands that formed you! You have made me wretched beyond expression. You have left me no power to consider whether I am just to you or not. Begone! Relieve me from the sight of your detested form.")</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/malformed_uri/index.html b/files/zh-cn/web/javascript/reference/errors/malformed_uri/index.html new file mode 100644 index 0000000000..40bb10ae1d --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/malformed_uri/index.html @@ -0,0 +1,65 @@ +--- +title: 'URIError: malformed URI sequence' +slug: Web/JavaScript/Reference/Errors/Malformed_URI +tags: + - JavaScript + - URLError + - 错误提示 +translation_of: Web/JavaScript/Reference/Errors/Malformed_URI +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="错误提示">错误提示</h2> + +<pre class="syntaxbox">URIError: malformed URI sequence (Firefox) +URIError: URI malformed (Chrome) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("URIError")}}</p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p>URI 编码和解码不成功。传递给 {{jsxref("decodeURI")}}、{{jsxref("encodeURI")}}、{{jsxref("encodeURIComponent")}} 或者 {{jsxref("decodeURIComponent")}} 这些函数的参数不合法,导致函数无法正确对其进行编解码。</p> + +<h2 id="示例">示例</h2> + +<h3 id="编码">编码</h3> + +<p>编码操作会将每一个字符实例替换为一到四个相对应的 UTF-8 编码形式的转义序列。如果试图编码一个非高-低位完整的代理字符,将会抛出一个 {{jsxref("URIError")}} 错误,例如:</p> + +<pre class="brush: js example-bad">encodeURI('\uD800'); +// "URIError: malformed URI sequence" + +encodeURI('\uDFFF'); +// "URIError: malformed URI sequence" +</pre> + +<p>高-低位完整的代理字符是没问题的,例如:</p> + +<pre class="brush: js example-good">encodeURI('\uD800\uDFFF'); +// "%F0%90%8F%BF"</pre> + +<h3 id="解码">解码</h3> + +<p>解码操作则是将已经经过编码的 URL 片段中的每一个转义序列替换为相对应的字符。如果相应的字符不存在,就会抛出错误:</p> + +<pre class="brush: js example-bad">decodeURIComponent('%E0%A4%A'); +// "URIError: malformed URI sequence" +</pre> + +<p>输入没问题的话,通常是下面这样:</p> + +<pre class="brush: js example-good">decodeURIComponent('JavaScript_%D1%88%D0%B5%D0%BB%D0%BB%D1%8B'); +// "JavaScript_шеллы"</pre> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li>{{jsxref("URIError")}}</li> + <li>{{jsxref("decodeURI")}}</li> + <li>{{jsxref("encodeURI")}}</li> + <li>{{jsxref("encodeURIComponent")}}</li> + <li>{{jsxref("decodeURIComponent")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/missing_bracket_after_list/index.html b/files/zh-cn/web/javascript/reference/errors/missing_bracket_after_list/index.html new file mode 100644 index 0000000000..d1da610184 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/missing_bracket_after_list/index.html @@ -0,0 +1,56 @@ +--- +title: 'SyntaxError: missing ] after element list' +slug: Web/JavaScript/Reference/Errors/Missing_bracket_after_list +tags: + - Errors + - JavaScript + - SyntaxError +translation_of: Web/JavaScript/Reference/Errors/Missing_bracket_after_list +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="信息">信息</h2> + +<pre class="syntaxbox">SyntaxError: missing ] after element list +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("SyntaxError")}}.</p> + +<h2 id="哪里出错了">哪里出错了?</h2> + +<p>数组初始化在某处出现了语法错误。比如缺少了右中括号("<code>]</code>") 或一个逗号 ("<code>,</code>")。</p> + +<h2 id="示例">示例</h2> + +<h3 id="不正确的数组初始化">不正确的数组初始化</h3> + +<pre class="brush: js example-bad">var list = [1, 2, + +var instruments = [ + "Ukulele", + "Guitar", + "Piano" +}; + +var data = [{foo: "bar"} {bar: "foo"}]; +</pre> + +<p>正确的是:</p> + +<pre class="brush: js example-good">var list = [1, 2]; + +var instruments = [ + "Ukulele", + "Guitar", + "Piano" +]; + +var data = [{foo: "bar"}, {bar: "foo"}];</pre> + +<h2 id="相关">相关</h2> + +<ul> + <li>{{jsxref("Array")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/missing_colon_after_property_id/index.html b/files/zh-cn/web/javascript/reference/errors/missing_colon_after_property_id/index.html new file mode 100644 index 0000000000..7cfdf7e318 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/missing_colon_after_property_id/index.html @@ -0,0 +1,76 @@ +--- +title: 'SyntaxError: missing : after property id' +slug: Web/JavaScript/Reference/Errors/Missing_colon_after_property_id +tags: + - JavaScript + - 语法错误 + - 错误提示 +translation_of: Web/JavaScript/Reference/Errors/Missing_colon_after_property_id +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="错误提示">错误提示</h2> + +<pre class="syntaxbox">SyntaxError: missing : after property id +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("SyntaxError")}}</p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p>当使用<a href="/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer">对象初始化</a>语法创建对象的时候,需要使用半角冒号 (:) 将属性键与属性值隔开。</p> + +<pre class="brush: js">var obj = { propertyKey: 'value' }; +</pre> + +<h2 id="示例">示例</h2> + +<h3 id="冒号与等号">冒号与等号</h3> + +<p>下面的代码会运行失败,原因是对象初始化语法中不允许使用等号来代替冒号。</p> + +<pre class="brush: js example-bad">var obj = { propertyKey = 'value' }; +// SyntaxError: missing : after property id +</pre> + +<p>修复方法就是使用冒号,或者是在对象创建之后使用方括号语法来为其设定新的属性。</p> + +<pre class="brush: js example-good">var obj = { propertyKey: 'value' }; + +// or alternatively + +var obj = { }; +obj['propertyKey'] = 'value'; +</pre> + +<h3 id="空属性">空属性</h3> + +<p>不能像下面这样创建空属性:</p> + +<pre class="brush: js example-bad">var obj = { propertyKey; }; +// SyntaxError: missing : after property id +</pre> + +<p>假如你需要创建一个无值属性,那么需要将它的值设置为 {{jsxref("null")}} 。</p> + +<pre class="brush: js example-good">var obj = { propertyKey: null };</pre> + +<h3 id="计算得来的属性">计算得来的属性</h3> + +<p>如果需要使用表达式来创建属性键,那么需要使用方括号。否则属性名称不会进行计算:</p> + +<pre class="brush: js example-bad">var obj = { 'b'+'ar': 'foo' }; +// SyntaxError: missing : after property id +</pre> + +<p>把计算表达式放置到方括号(<code>[]</code>)中:</p> + +<pre class="brush: js example-good">var obj = { ['b'+'ar']: 'foo' };</pre> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer">Object initializer</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/missing_curly_after_function_body/index.html b/files/zh-cn/web/javascript/reference/errors/missing_curly_after_function_body/index.html new file mode 100644 index 0000000000..36761114d1 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/missing_curly_after_function_body/index.html @@ -0,0 +1,66 @@ +--- +title: 'SyntaxError: missing } after function body' +slug: Web/JavaScript/Reference/Errors/Missing_curly_after_function_body +tags: + - JavaScript + - 语法错误 + - 错误提示 +translation_of: Web/JavaScript/Reference/Errors/Missing_curly_after_function_body +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="错误提示">错误提示</h2> + +<pre class="syntaxbox">SyntaxError: missing } after function body +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("SyntaxError")}}</p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p>在创建函数的时候出现了语法错误。检查一下闭合花括号以及括号的顺序是否正确。将代码进行缩进或者美化可以让你更容易找到问题之所在。</p> + +<h2 id="示例">示例</h2> + +<h3 id="遗漏的闭合花括号">遗漏的闭合花括号</h3> + +<p>通常是因为在函数声明中遗漏了花括号:</p> + +<pre class="brush: js example-bad">var charge = function() { + if (sunny) { + useSolarCells(); + } else { + promptBikeRide(); +}; +</pre> + +<p>正确的应该是这样的:</p> + +<pre class="brush: js example-good">var charge = function() { + if (sunny) { + useSolarCells(); + } else { + promptBikeRide(); + } +};</pre> + +<p>当使用立即调用函数表达式({{Glossary("IIFE")}})、<a href="/en-US/docs/Web/JavaScript/Closures">闭包</a>或者其他使用了大量的花括号以及括号的结构体的时候,问题会更加隐蔽。</p> + +<pre class="brush: js example-bad">(function() { if (true) { return false; } ); +</pre> + +<p>通常将代码语句按照层级缩进以及对缩进进行复核会有助于错误的发现。</p> + +<pre class="brush: js example-good">(function() { + if (true) { + return false; + } +});</pre> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Guide/Functions">Functions</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/missing_curly_after_property_list/index.html b/files/zh-cn/web/javascript/reference/errors/missing_curly_after_property_list/index.html new file mode 100644 index 0000000000..4c6a06e4e0 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/missing_curly_after_property_list/index.html @@ -0,0 +1,51 @@ +--- +title: 'SyntaxError: missing } after property list' +slug: Web/JavaScript/Reference/Errors/Missing_curly_after_property_list +tags: + - Errors + - JavaScript + - SyntaxError +translation_of: Web/JavaScript/Reference/Errors/Missing_curly_after_property_list +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="信息">信息</h2> + +<pre class="syntaxbox">SyntaxError: missing } after property list +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("SyntaxError")}}</p> + +<h2 id="哪里出错了">哪里出错了?</h2> + +<p>在<a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/Object_initializer">对象初始化</a>的时候语法出错了。可能是遗漏了一个大括号,也可能是遗漏了逗号。还要检查是否以正确的顺序关闭了大括号或括号。 缩进或格式化代码也许可以更好帮助你看清这些芜杂的。</p> + +<h2 id="示例">示例</h2> + +<h3 id="遗漏的逗号">遗漏的逗号</h3> + +<p>很多情况下,对象初始值代码会中缺少逗号:</p> + +<pre class="brush: js example-bad">var obj = { + a: 1, + b: { myProp: 2 } + c: 3 +}; +</pre> + +<p>正确的是:</p> + +<pre class="brush: js example-good">var obj = { + a: 1, + b: { myProp: 2 }, + c: 3 +}; +</pre> + +<h2 id="相关">相关</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer">Object initializer</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/missing_formal_parameter/index.html b/files/zh-cn/web/javascript/reference/errors/missing_formal_parameter/index.html new file mode 100644 index 0000000000..9d8a069258 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/missing_formal_parameter/index.html @@ -0,0 +1,80 @@ +--- +title: 'SyntaxError: missing formal parameter' +slug: Web/JavaScript/Reference/Errors/Missing_formal_parameter +tags: + - JavaScript + - 类型错误 + - 错误 + - 错误提示 +translation_of: Web/JavaScript/Reference/Errors/Missing_formal_parameter +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="信息提示">信息提示</h2> + +<pre class="syntaxbox">SyntaxError: missing formal parameter (Firefox) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("SyntaxError")}}</p> + +<h2 id="哪里出错了">哪里出错了?</h2> + +<p>“形式参数” 是 “函数参数”一种更好的说法。函数声明缺少合法的参数定义。在函数声明中,参数定义必须为标记符({{Glossary("Identifier", "identifiers")}}), 而不是任何类似于数字、字符串或者对象的值。函数声明和函数调用是两个不同的步骤。函数声明中的参数需要使用标记符,而仅仅在函数被呼叫(调用)的时候才可以传入其所需要的值。</p> + +<p>在 {{glossary("JavaScript")}} 中,标记符只能由字母、数字、"$" 以及 "_" 构成,并且不能以数字开头。标记符与<strong>字符串</strong>的区别在于字符串是数据,而标记符属于代码的一部分。</p> + +<h2 id="示例">示例</h2> + +<p>在构造一个函数的时候,函数参数必须为标记符。下面列举的函数声明都是无效的,因为它们在参数部分使用的是数值:</p> + +<pre class="brush: js example-bad highlight:[1,6,11]">function square(3) { + return number * number; +}; +// SyntaxError: missing formal parameter + +function greet("Howdy") { + return greeting; +}; +// SyntaxError: missing formal parameter + +function log({ obj: "value"}) { + console.log(arg) +}; +// SyntaxError: missing formal parameter +</pre> + +<p>需要在函数声明中使用标记符:</p> + +<pre class="brush: js example-good highlight:[1,5,9]">function square(number) { + return number * number; +}; + +function greet(greeting) { + return greeting; +}; + +function log(arg) { + console.log(arg) +};</pre> + +<p>之后可以传入你想要传入的实际参数调用函数:</p> + +<pre class="brush: js">square(2); // 4 + +greet("Howdy"); // "Howdy" + +log({obj: "value"}); // Object { obj: "value" } +</pre> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li>Other errors regarding formal parameters: + <ul> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Errors/Malformed_formal_parameter">SyntaxError: Malformed formal parameter</a></li> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Errors/Redeclared_parameter">SyntaxError: redeclaration of formal parameter "x"</a></li> + </ul> + </li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/missing_initializer_in_const/index.html b/files/zh-cn/web/javascript/reference/errors/missing_initializer_in_const/index.html new file mode 100644 index 0000000000..f87d78f746 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/missing_initializer_in_const/index.html @@ -0,0 +1,57 @@ +--- +title: 'SyntaxError: missing = in const declaration' +slug: Web/JavaScript/Reference/Errors/Missing_initializer_in_const +tags: + - JavaScript + - 语法错误 + - 错误提示 +translation_of: Web/JavaScript/Reference/Errors/Missing_initializer_in_const +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="错误提示">错误提示</h2> + +<pre class="syntaxbox">SyntaxError: missing = in const declaration (Firefox) +SyntaxError: Missing initializer in const declaration (Chrome) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("SyntaxError")}}</p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p>常量指的是在程序正常运行过程中不能被修改的值。它的值不能通过二次赋值来改变,同时也不能被再次声明。在 JavaScript 中,常量在声明时使用 <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/const">const</a></code> 关键字来修饰。常量需要初始化器;也就是说,必须在声明的同时为其赋值(鉴于常量值指定之后不能再进行修改,这样做是容易理解的)。</p> + +<h2 id="示例">示例</h2> + +<h3 id="初始值缺失">初始值缺失</h3> + +<p>不同于 var 或 let 关键字,必须在常量声明中为其赋值。下面的例子中会报错:</p> + +<pre class="brush: js example-bad">const COLUMNS; +// SyntaxError: missing = in const declaration</pre> + +<h3 id="错误修正">错误修正</h3> + +<p>修正这个问题的方法有很多种。仔细检查对于问题中的常量,你想要达到什么目标。</p> + +<h4 id="添加常量值">添加常量值</h4> + +<p>在声明语句中为其赋值:</p> + +<pre class="brush: js example-good">const COLUMNS = 80;</pre> + +<h4 id="const_let_还是_var"><code>const</code>, <code>let</code> 还是 <code>var?</code></h4> + +<p>如果你并不想声明一个常量值,那么就不要使用 const 关键字。或许你想要的是使用 <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/let">let</a></code> 来声明一个块状作用域变量或者是用 <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/var">var</a> </code>声明一个全局变量。这两者都不需要设置初始值。</p> + +<pre class="brush: js example-good">let columns; +</pre> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/let">let</a></code></li> + <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/var">var</a></code></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/missing_name_after_dot_operator/index.html b/files/zh-cn/web/javascript/reference/errors/missing_name_after_dot_operator/index.html new file mode 100644 index 0000000000..634008c21b --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/missing_name_after_dot_operator/index.html @@ -0,0 +1,67 @@ +--- +title: 'SyntaxError: missing name after . operator' +slug: Web/JavaScript/Reference/Errors/Missing_name_after_dot_operator +tags: + - JavaScript + - 语法错误 + - 错误提示 +translation_of: Web/JavaScript/Reference/Errors/Missing_name_after_dot_operator +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="错误提示">错误提示</h2> + +<pre class="syntaxbox">SyntaxError: missing name after . operator +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("SyntaxError")}}</p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p>点操作符 (<code>.</code>) 用于<a href="/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors">属性访问</a>。你需要指定要访问的属性名称。对于属性名称是运算表达式的属性访问,需要将属性访问的方式从点操作符转换为方括号操作符。这样你就可以在其中进行表达式运算了。或许你想要进行字符串拼接操作?那么请使用加号操作符(+)。请参考下面的示例。</p> + +<h2 id="示例">示例</h2> + +<h3 id="属性访问">属性访问</h3> + +<p>在 JavaScript 中,<a href="/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors">属性访问器</a>或者使用点操作符 (.),或者使用方括号操作符 ([]),但是二者不能同时使用。方括号操作符允许属性名称为运算表达式的属性访问。</p> + +<pre class="brush: js example-bad">var obj = { foo: { bar: "baz", bar2: "baz2" } }; +var i = 2; + +obj.[foo].[bar] +// SyntaxError: missing name after . operator + +obj.foo."bar"+i; +// SyntaxError: missing name after . operator +</pre> + +<p>为了进行代码修复,你需要向下面这样来访问对象:</p> + +<pre class="brush: js example-good">obj.foo.bar; // "baz" +// or alternatively +obj["foo"]["bar"]; // "baz" + +// computed properties require square brackets +obj.foo["bar" + i]; // "baz2" +</pre> + +<h3 id="属性访问与字符串拼接">属性访问与字符串拼接</h3> + +<p>如果你是中其他编程语言(如 {{Glossary("PHP")}})转到 JavaScript 的,很容易将点操作符 (.) 与连接操作符 (+) 混用:</p> + +<pre class="brush: js example-bad">console.log("Hello" . "world"); + +// SyntaxError: missing name after . operator</pre> + +<p>相反要使用加号来进行字符串拼接:</p> + +<pre class="brush: js example-good">console.log("Hello" + "World");</pre> + +<h2 id="相关页面">相关页面</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors">属性访问器</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/missing_parenthesis_after_argument_list/index.html b/files/zh-cn/web/javascript/reference/errors/missing_parenthesis_after_argument_list/index.html new file mode 100644 index 0000000000..5d3dfe8809 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/missing_parenthesis_after_argument_list/index.html @@ -0,0 +1,42 @@ +--- +title: 'SyntaxError: missing ) after argument list' +slug: Web/JavaScript/Reference/Errors/Missing_parenthesis_after_argument_list +tags: + - JavaScript + - 语法错误 + - 错误 +translation_of: Web/JavaScript/Reference/Errors/Missing_parenthesis_after_argument_list +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="消息">消息</h2> + +<pre class="syntaxbox">语法错误: 参数列表后面缺少 ) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("SyntaxError")}}.</p> + +<h2 id="什么地方出错了">什么地方出错了?</h2> + +<p>有一个函数在调用时出现错误。这可能是一个错误,丢失运算符或者转义字符等。</p> + +<h2 id="示例">示例</h2> + +<p>因为没有使用 ”+“ 操作符来连接字符串,JavaScript 认为 <code>log</code> 函数的参数的值只是 “<code>PI: ”,在这种情况下,它应该用一个右括号作为结束。</code></p> + +<pre class="brush: js example-bad">console.log("PI: " Math.PI); +// SyntaxError: missing ) after argument list +</pre> + +<p>你可以正确的调用 <code>log</code> 函数通过加上 ”+“ 操作符。</p> + +<pre class="brush: js example-good">console.log("PI: " + Math.PI); +// "PI: 3.141592653589793"</pre> + +<h2 id="相关页面">相关页面</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Guide/Functions">Functions</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/missing_parenthesis_after_condition/index.html b/files/zh-cn/web/javascript/reference/errors/missing_parenthesis_after_condition/index.html new file mode 100644 index 0000000000..e2db63da04 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/missing_parenthesis_after_condition/index.html @@ -0,0 +1,69 @@ +--- +title: 'SyntaxError: missing ) after condition' +slug: Web/JavaScript/Reference/Errors/Missing_parenthesis_after_condition +tags: + - JavaScript + - 语法错误 + - 错误 +translation_of: Web/JavaScript/Reference/Errors/Missing_parenthesis_after_condition +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="错误提示">错误提示</h2> + +<pre class="syntaxbox">SyntaxError: missing ) after condition +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("SyntaxError")}}</p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p>这个错误与 if 条件语句是如何写的有关。在任何编程语言中,代码都需要做出一些判断,然后根据不同的判断结果来执行不同的操作。if 语句会在其指定的判断条件为真的时候执行。在 JavaScript 中,条件表达式必须出现在 if 关键字后面的一对括号中,像下面这样:</p> + +<pre class="brush: js">if (condition) { + // do something if the condition is true +}</pre> + +<h2 id="示例">示例</h2> + +<p>下面这种情况的出现可能只是出于大意,需要仔细检查代码中的括号。</p> + +<pre class="brush: js example-bad">if (3 > Math.PI { + console.log("wait what?"); +} + +// SyntaxError: missing ) after condition +</pre> + +<p>修复代码的方法就是添加闭合条件表达式的右括号。</p> + +<pre class="brush: js example-good">if (3 > Math.PI) { + console.log("wait what?"); +}</pre> + +<p>如果你是从其他语言转到 JavaScript 的,那么很容易在 JavaScript 中使用与之含义不同或者没有任何意义的关键字。</p> + +<pre class="brush: js example-bad">if (done is true) { + console.log("we are done!"); +} + +// SyntaxError: missing ) after condition +</pre> + +<p>相反你需要使用正确的<a href="/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators">比较操作符</a>, 如下:</p> + +<pre class="brush: js example-good">if (done === true) { + console.log("we are done!"); +}</pre> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/if...else">if...else</a></code></li> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators">比较操作符</a></li> + <li> + <p><a href="/en-US/docs/Learn/JavaScript/Building_blocks/conditionals">在代码中做判断 — 条件表达式</a></p> + </li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/missing_semicolon_before_statement/index.html b/files/zh-cn/web/javascript/reference/errors/missing_semicolon_before_statement/index.html new file mode 100644 index 0000000000..e3e6a98c88 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/missing_semicolon_before_statement/index.html @@ -0,0 +1,81 @@ +--- +title: 'SyntaxError: missing ; before statement' +slug: Web/JavaScript/Reference/Errors/Missing_semicolon_before_statement +translation_of: Web/JavaScript/Reference/Errors/Missing_semicolon_before_statement +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="信息">信息</h2> + +<pre class="syntaxbox">SyntaxError: Expected ';' (Edge)</pre> + +<pre class="syntaxbox"> SyntaxError: missing ; before statement (Firefox) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("SyntaxError")}}.</p> + +<h2 id="哪里出错了">哪里出错了?</h2> + +<p>某个地方丢失了一个(<code>;</code>)。 <a href="/en-US/docs/Web/JavaScript/Reference/Statements">JavaScript 语句</a>必须以分号结束。 有一些是由 <a href="/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Automatic_semicolon_insertion">自动分号插入(ASI)</a> 的使用所影响到的, 但在这种情况下,你需要提供一个分号,以便于JavaScript可以将源代码解析正确。然而,通常情况下,这个错误只是另一个错误一个导致的,如不正确转义字符串,使用var的错误。你也可能有太多的括号的地方。当出现此错误时,仔细检查语法。</p> + +<h2 id="例子">例子</h2> + +<p> </p> + +<p>转义字符串</p> + +<p>这个错误可能发生不正确时容易转义字符串JavaScript引擎是希望你的字符串的末尾已经。例如:</p> + +<pre class="brush: js example-bad">var foo = 'Tom's bar'; +// SyntaxError: missing ; before statement</pre> + +<p>你可以用双引号,或者用\转义:</p> + +<pre class="brush: js example-good">var foo = "Tom's bar"; +var foo = 'Tom\'s bar'; +</pre> + +<h3 id="用var声明属性">用var声明属性</h3> + +<p>你不能将带有变量声明的对象或数组的属性用<code>var</code>来声明。</p> + +<pre class="brush: js example-bad">var obj = {}; +var obj.foo = "hi"; // SyntaxError missing ; before statement + +var array = []; +var array[0] = "there"; // SyntaxError missing ; before statement +</pre> + +<p>而是省略掉<code>var</code>关键词:</p> + +<pre class="brush: js example-good">var obj = {}; +obj.foo = "hi"; + +var array = []; +array[0] = "there"; +</pre> + +<h3 id="不推荐使用的关键字">不推荐使用的关键字</h3> + +<p>如果你用的是另一种编程语言,那么在javaScript中使用不具有相同或完全没有意义的关键字也是很常见的:</p> + +<pre class="brush: js example-bad line-numbers language-js"><code class="language-js">def <span class="function token">print</span><span class="punctuation token">(</span>info<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>info<span class="punctuation token">)</span><span class="punctuation token">;</span> +<span class="punctuation token">}</span><span class="punctuation token">;</span> <span class="comment token">// SyntaxError missing ; before statement</span></code></pre> + +<p>因此,建议使用<code>function</code>而不是<code>def</code>:</p> + +<pre class="brush: js example-good line-numbers language-js"><code class="language-js"><span class="keyword token">function</span> <span class="function token">print</span><span class="punctuation token">(</span>info<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>info<span class="punctuation token">)</span><span class="punctuation token">;</span> +<span class="punctuation token">}</span><span class="punctuation token">;</span></code></pre> + +<p> </p> + +<h2 id="参考">参考</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Automatic_semicolon_insertion">自动分号插入(ASI)</a> </li> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Statements">JavaScript 语句</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/more_arguments_needed/index.html b/files/zh-cn/web/javascript/reference/errors/more_arguments_needed/index.html new file mode 100644 index 0000000000..b042af14bb --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/more_arguments_needed/index.html @@ -0,0 +1,48 @@ +--- +title: 'TypeError: More arguments needed' +slug: Web/JavaScript/Reference/Errors/More_arguments_needed +tags: + - Errors + - JavaScript + - TypeError +translation_of: Web/JavaScript/Reference/Errors/More_arguments_needed +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="信息">信息</h2> + +<pre class="syntaxbox">TypeError: Object.create requires more than 0 arguments +TypeError: Object.setPrototypeOf requires more than 1 argument +TypeError: Object.defineProperties requires more than 0 arguments +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("TypeError")}}.</p> + +<h2 id="哪里出错了">哪里出错了?</h2> + +<p>调用函数的时候出现了错误。需要提供更多的参数。</p> + +<h2 id="示例">示例</h2> + +<p>{{jsxref("Object.create()")}} 方法要求至少有一个参数,而 {{jsxref("Object.setPrototypeOf()")}} 方法要求至少有两个参数:</p> + +<pre class="brush: js example-bad">var obj = Object.create(); +// TypeError: Object.create requires more than 0 arguments + +var obj = Object.setPrototypeOf({}); +// TypeError: Object.setPrototypeOf requires more than 1 argument +</pre> + +<p>你可以将 {{jsxref("null")}} 设置为原型:</p> + +<pre class="brush: js example-good">var obj = Object.create(null); + +var obj = Object.setPrototypeOf({}, null);</pre> + +<h2 id="相关">相关</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Guide/Functions">Functions</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/negative_repetition_count/index.html b/files/zh-cn/web/javascript/reference/errors/negative_repetition_count/index.html new file mode 100644 index 0000000000..780bdabcf4 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/negative_repetition_count/index.html @@ -0,0 +1,45 @@ +--- +title: 'RangeError: repeat count must be non-negative' +slug: Web/JavaScript/Reference/Errors/Negative_repetition_count +tags: + - Errors + - JavaScript + - RangeError + - 错误 +translation_of: Web/JavaScript/Reference/Errors/Negative_repetition_count +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="信息">信息</h2> + +<pre class="syntaxbox">RangeError: repeat count must be non-negative (Firefox) +RangeError: Invalid count value (Chrome) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("RangeError")}}</p> + +<h2 id="发生了什么">发生了什么?</h2> + +<p>代码中使用了 {{jsxref("String.prototype.repeat()")}}方法。<span class="short_text" id="result_box" lang="zh-CN"><span>它有一个计数参数,表示重复该字符串的次数</span></span>。该参数必须在 0 及正 {{jsxref("Infinity")}} 之间,且不能为负数。该值的合法范围可以这样表示: [0, +∞)。</p> + +<h2 id="示例">示例</h2> + +<h3 id="无效的">无效的</h3> + +<pre class="brush: js example-bad">'abc'.repeat(-1); // RangeError </pre> + +<h3 id="有效的">有效的</h3> + +<pre class="brush: js example-good">'abc'.repeat(0); // '' +'abc'.repeat(1); // 'abc' +'abc'.repeat(2); // 'abcabc' +'abc'.repeat(3.5); // 'abcabcabc' (count will be converted to integer) +</pre> + +<h2 id="See_also">See also</h2> + +<ul> + <li>{{jsxref("String.prototype.repeat()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/no_non-null_object/index.html b/files/zh-cn/web/javascript/reference/errors/no_non-null_object/index.html new file mode 100644 index 0000000000..6369e8eaf1 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/no_non-null_object/index.html @@ -0,0 +1,65 @@ +--- +title: 'TypeError: "x" is not a non-null object' +slug: Web/JavaScript/Reference/Errors/No_non-null_object +tags: + - JavaScript + - 类型错误 + - 错误提示 +translation_of: Web/JavaScript/Reference/Errors/No_non-null_object +--- +<div>{{JSSidebar("Errors")}}</div> + +<h2 id="提示信息">提示信息</h2> + +<pre class="syntaxbox">TypeError: "x" is not a non-null object (Firefox) +TypeError: Property description must be an object: "x" (Chrome) +TypeError: Invalid value used in weak set (Chrome) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("TypeError")}}</p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p>在期待出现对象类型的值的地方而没有提供。{{jsxref("null")}} 不是对象类型,因此不起作用。必须在给定的场景下提供一个合适的对象。</p> + +<h2 id="示例">示例</h2> + +<h3 id="期望的属性描述器">期望的属性描述器</h3> + +<p>当使用诸如 {{jsxref("Object.create()")}} 或 {{jsxref("Object.defineProperty()")}} 及{jsxref("Object.defineProperties()")}} 方法时,可选的属性描述器参数需要提供一个描述器对象。提供非对象类型的值(例如数字)将会报错:</p> + +<pre class="brush: js example-bad">Object.defineProperty({}, 'key', 1); +// TypeError: 1 is not a non-null object + +Object.defineProperty({}, 'key', null); +// TypeError: null is not a non-null object +</pre> + +<p>一个合法的描述器对象类似于下面这样:</p> + +<pre class="brush: js example-good">Object.defineProperty({}, 'key', { value: 'foo', writable: false }); +</pre> + +<h3 id="WeakMap_和_WeakSet_对象需要对象类型的键"><code>WeakMap</code> 和 <code>WeakSet</code> 对象需要对象类型的键</h3> + +<p>{{jsxref("WeakMap")}} 和 {{jsxref("WeakSet")}} 对象只能存储对象类型的键,而不能使用其他类型的。</p> + +<pre class="brush: js example-bad">var ws = new WeakSet(); +ws.add('foo'); +// TypeError: "foo" is not a non-null object</pre> + +<p>用对象类型的值来替换:</p> + +<pre class="brush: js example-good">ws.add({foo: 'bar'}); +ws.add(window); +</pre> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li>{{jsxref("Object.create()")}}</li> + <li>{{jsxref("Object.defineProperty()")}}, {{jsxref("Object.defineProperties()")}}</li> + <li>{{jsxref("WeakMap")}}, {{jsxref("WeakSet")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/no_properties/index.html b/files/zh-cn/web/javascript/reference/errors/no_properties/index.html new file mode 100644 index 0000000000..5e904dd8ab --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/no_properties/index.html @@ -0,0 +1,36 @@ +--- +title: 'TypeError: "x" has no properties' +slug: Web/JavaScript/Reference/Errors/No_properties +translation_of: Web/JavaScript/Reference/Errors/No_properties +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="错误信息">错误信息</h2> + +<pre class="syntaxbox">TypeError: null has no properties +TypeError: undefined has no properties +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("TypeError")}}.</p> + +<h2 id="哪里出错了">哪里出错了?</h2> + +<p> {{jsxref("null")}} 和 {{jsxref("undefined")}}中,没有你需要的属性。</p> + +<h2 id="例子">例子</h2> + +<pre class="brush: js example-bad">null.foo; +// 错误类型: null没有这个属性 + +undefined.bar; +// 错误类型: undefined没有这个属性 +</pre> + +<h2 id="参考">参考</h2> + +<ul> + <li>{{jsxref("null")}}</li> + <li>{{jsxref("undefined")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/no_variable_name/index.html b/files/zh-cn/web/javascript/reference/errors/no_variable_name/index.html new file mode 100644 index 0000000000..4b69a5cec1 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/no_variable_name/index.html @@ -0,0 +1,83 @@ +--- +title: 'SyntaxError: missing variable name' +slug: Web/JavaScript/Reference/Errors/No_variable_name +tags: + - JavaScript + - 语法错误 + - 错误提示 +translation_of: Web/JavaScript/Reference/Errors/No_variable_name +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="错误提示">错误提示</h2> + +<pre class="syntaxbox">SyntaxError: missing variable name (Firefox) +SyntaxError: Unexpected token = (Chrome)</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("SyntaxError")}}</p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p>缺失变量名。这有很大的可能性是由你的代码中的语法错误造成的。也许是什么地方的逗号出了问题,或者是你正在绞尽脑汁想一个合适的名字。完全可以理解!为变量起名确实让人头疼。</p> + +<h2 id="示例">示例</h2> + +<h3 id="变量名缺失">变量名缺失</h3> + +<pre class="brush: js example-bad">var = "foo"; +</pre> + +<p>为变量起一个合适的明确确实不是一件容易的事情。这种经历每个人都遇到过。</p> + +<pre class="brush: js example-good">var ohGodWhy = "foo";</pre> + +<h3 id="保留字不能作为变量名">保留字不能作为变量名</h3> + +<p>有一些名称是作为<a href="/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords">保留字</a>而存在的。不好意思,你不能使用!:(</p> + +<pre class="brush: js example-bad">var debugger = "whoop"; +// SyntaxError: missing variable name +</pre> + +<h3 id="一次性声明多个变量">一次性声明多个变量</h3> + +<p>在一次性声明多个变量的时候要特别留意逗号。看看是否有多余的逗号?以及是否不小心把逗号用作了分号?</p> + +<pre class="brush: js example-bad">var x, y = "foo", +var x, = "foo" + +var first = document.getElementById('one'), +var second = document.getElementById('two'), + +// SyntaxError: missing variable name +</pre> + +<p>修复后的代码:</p> + +<pre class="brush: js example-good">var x, y = "foo"; +var x = "foo"; + +var first = document.getElementById('one'); +var second = document.getElementById('two');</pre> + +<h3 id="数组">数组</h3> + +<p>在 JavaScript 中 {{jsxref("Array")}} 字面量需要使用方括号将值包裹起来。下面这样是不对的:</p> + +<pre class="brush: js example-bad">var arr = 1,2,3,4,5; +// SyntaxError: missing variable name +</pre> + +<p>这样写才是正确的:</p> + +<pre class="brush: js example-good">var arr = [1,2,3,4,5];</pre> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li><a href="http://wiki.c2.com/?GoodVariableNames">Good variable names</a></li> + <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/var">var</a></code></li> + <li><a href="/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#Declarations">Variable declarations in the JavaScript Guide</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/non_configurable_array_element/index.html b/files/zh-cn/web/javascript/reference/errors/non_configurable_array_element/index.html new file mode 100644 index 0000000000..d38da28248 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/non_configurable_array_element/index.html @@ -0,0 +1,81 @@ +--- +title: 'TypeError: can''t delete non-configurable array element' +slug: Web/JavaScript/Reference/Errors/Non_configurable_array_element +tags: + - JavaScript + - 错误提示 +translation_of: Web/JavaScript/Reference/Errors/Non_configurable_array_element +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="错误提示">错误提示</h2> + +<pre class="syntaxbox">TypeError: can't delete non-configurable array element (Firefox) +TypeError: Cannot delete property '2' of [object Array] (Chrome) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("TypeError")}}</p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p>这个错误提示发生于当试图<a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length#Shortening_an_array">缩短一个数组的长度</a>的时候,其中有元素是不可配置的(<a href="/en-US/docs/Web/JavaScript/Data_structures#Properties">non-configurable</a>)。正常情况下,缩短数组的长度,则超出限度的元素会被删除,而这里指的是这种操作失效的情况。</p> + +<p><code><font face="Open Sans, arial, x-locale-body, sans-serif"> </font>configurable</code> 特性控制着属性是否可以从对象中删除,以及它的特性(除了 writable 之外)是否可以发生改变。</p> + +<p>通常,使用<a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Syntax">数组初始化语句</a>创建的对象是可配置的,而通过 {{jsxref("Object.defineProperty()")}} 创建的属性,默认则是不可配置的。</p> + +<h2 id="示例">示例</h2> + +<h3 id="通过_Object.defineProperty_创建的不可配置属性"><code><font face="x-locale-heading-primary, zillaslab, Palatino, Palatino Linotype, x-locale-heading-secondary, serif">通过 </font>Object.defineProperty 创建的不可配置属性</code></h3> + +<p>使用 {{jsxref("Object.defineProperty()")}} 且在没有明确将属性设定为可配置的情况下,默认可以创建不可配置属性。</p> + +<pre class="brush: js example-bad">var arr = []; +Object.defineProperty(arr, 0, {value: 0}); +Object.defineProperty(arr, 1, {value: "1"}); + +arr.length = 1; +// TypeError: can't delete non-configurable array element +</pre> + +<p>如果想要缩短数组长度的话,需要将其中的元素设置为可配置的。</p> + +<pre class="brush: js example-good">var arr = []; +Object.defineProperty(arr, 0, {value: 0, configurable: true}); +Object.defineProperty(arr, 1, {value: "1", configurable: true}); + +arr.length = 1; +</pre> + +<h3 id="密封的数组"><code>密封的数组</code></h3> + +<p> {{jsxref("Object.seal()")}} 函数会将数组中现存的所有元素标记为不可配置。</p> + +<pre class="brush: js example-bad">var arr = [1,2,3]; +Object.seal(arr); + +arr.length = 1; +// TypeError: can't delete non-configurable array element +</pre> + +<p>(为了解决上述问题,)或者是移除 {{jsxref("Object.seal()")}} 调用,或者将数组拷贝一份。在拷贝数组的情况下,缩短备份数组的长度并不会修改原始数组的长度。</p> + +<pre class="brush: js example-good">var arr = [1,2,3]; +Object.seal(arr); + +// Copy the initial array to shorten the copy +var copy = Array.from(arr); +copy.length = 1; +// arr.length == 3 +</pre> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Data_structures#Properties">[[Configurable]]</a></li> + <li>{{jsxref("Array.length")}}</li> + <li>{{jsxref("Object.defineProperty()")}}</li> + <li>{{jsxref("Object.seal()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/not_a_codepoint/index.html b/files/zh-cn/web/javascript/reference/errors/not_a_codepoint/index.html new file mode 100644 index 0000000000..769e6daa0c --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/not_a_codepoint/index.html @@ -0,0 +1,51 @@ +--- +title: 'RangeError: argument is not a valid code point' +slug: Web/JavaScript/Reference/Errors/Not_a_codepoint +translation_of: Web/JavaScript/Reference/Errors/Not_a_codepoint +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="错误信息">错误信息</h2> + +<pre class="syntaxbox">RangeError: {0} is not a valid code point (Firefox) +RangeError: Invalid code point {0} (Chrome) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("RangeError")}}</p> + +<h2 id="什么地方出错了?">什么地方出错了?</h2> + +<p> {{jsxref("String.fromCodePoint()")}} 这个方法只能接受有效的码位(code point) 。</p> + +<p>码位( <a href="https://en.wikipedia.org/wiki/Code_point">code point</a>)是组成码空间(或代码页)的数值,范围是 0 到 0x10FFFF。</p> + +<p> {{jsxref("NaN")}},负整数(-1),非整数(3.14),或编号大于0x10FFFF (1114111) 的字符,无法使用该方法。</p> + +<h2 id="范例">范例</h2> + +<h3 id="无效的例子">无效的例子</h3> + +<pre class="brush: js example-bad">String.fromCodePoint('_'); // RangeError +String.fromCodePoint(Infinity); // RangeError +String.fromCodePoint(-1); // RangeError +String.fromCodePoint(3.14); // RangeError +String.fromCodePoint(3e-2); // RangeError +String.fromCodePoint(NaN); // RangeError</pre> + +<h3 id="有效的例子">有效的例子</h3> + +<pre class="brush: js example-good">String.fromCodePoint(42); // "*" +String.fromCodePoint(65, 90); // "AZ" +String.fromCodePoint(0x404); // "\u0404" +String.fromCodePoint(0x2F804); // "\uD87E\uDC04" +String.fromCodePoint(194564); // "\uD87E\uDC04" +String.fromCodePoint(0x1D306, 0x61, 0x1D307) // "\uD834\uDF06a\uD834\uDF07" +</pre> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{jsxref("String.fromCodePoint()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/not_a_constructor/index.html b/files/zh-cn/web/javascript/reference/errors/not_a_constructor/index.html new file mode 100644 index 0000000000..60279ed3c8 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/not_a_constructor/index.html @@ -0,0 +1,95 @@ +--- +title: 'TypeError: "x" is not a constructor' +slug: Web/JavaScript/Reference/Errors/Not_a_constructor +tags: + - Errors + - JavaScript + - TypeError +translation_of: Web/JavaScript/Reference/Errors/Not_a_constructor +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="信息">信息</h2> + +<pre class="syntaxbox">TypeError: "x" is not a constructor + +TypeError: Math is not a constructor +TypeError: JSON is not a constructor +TypeError: Symbol is not a constructor +TypeError: Reflect is not a constructor +TypeError: Intl is not a constructor +TypeError: SIMD is not a constructor +TypeError: Atomics is not a constructor +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("TypeError")}}</p> + +<h2 id="哪里出错了">哪里出错了?</h2> + +<p>是因为尝试将不是构造器的对象或者变量来作为构造器使用。参考 {{Glossary("constructor")}} 或者 <a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/new"><code>new</code> operator</a> 来了解什么是构造器。</p> + +<p>有很多的全局对象比如 {{jsxref("String")}}、{{jsxref("Array")}} 等等都是可以使用 <code>new</code> 操作符的构造器。但是有一些全局对象并不是,且其属性和方法都是<a href="https://en.wikipedia.org/wiki/Method_(computer_programming)#Static_methods">静态</a>的。下面的 JavaScript 标准内置对象都不是构造器:{{jsxref("Math")}},{{jsxref("JSON")}},{{jsxref("Symbol")}},{{jsxref("Reflect")}},{{jsxref("Intl")}},{{jsxref("SIMD")}},{{jsxref("Atomics")}}。</p> + +<p><a href="/zh-CN/docs/Web/JavaScript/Reference/Statements/function*">Generator functions</a> 也不能作为构造器来使用。</p> + +<h2 id="示例">示例</h2> + +<h3 id="无效的">无效的</h3> + +<pre class="brush: js example-bad">var Car = 1; +new Car(); +// TypeError: Car is not a constructor + +new Math(); +// TypeError: Math is not a constructor + +new Symbol(); +// TypeError: Symbol is not a constructor + +function* f() {}; +var obj = new f; +// TypeError: f is not a constructor +</pre> + +<h3 id="一个构造器">一个构造器</h3> + +<p>假设你想为汽车创建一个对象类型。 你希望此类型的对象被称为 <code>car</code>,并且您希望它具有make,model 和 year 属性。 为此,你编写以下函数:</p> + +<pre class="brush: js">function Car(make, model, year) { + this.make = make; + this.model = model; + this.year = year; +} +</pre> + +<p>现在你可以创建一个名为 <code>mycar</code> 的对象,如下所示:</p> + +<pre class="brush: js">var mycar = new Car("Eagle", "Talon TSi", 1993);</pre> + +<h3 id="关于_Promises">关于 Promises </h3> + +<p>当返回了一个 immediately-resolved 或者 immediately-rejected Promise 的时候,你根本不需要去创建、操作一个新的 Promise 对象。</p> + +<p>这是不合法的(<a href="/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm/Promise#Constructor">Promise constructor</a> 被错误的调用了)且会抛出一个 错误 <code>TypeError: this is not a constructor</code> exception:</p> + +<pre class="brush: js example-bad">return new Promise.resolve(true); +</pre> + +<p>使用<a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve"> Promise.resolve()</a> 或者 <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/reject">Promise.reject()</a> 静态方法来代替:</p> + +<pre class="brush: js">// 这是合法的,但是没必要这么长: +return new Promise((resolve, reject) => { resolve(true); }) + +// 用静态方法来代替: +return Promise.resolve(true); +return Promise.reject(false); +</pre> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{Glossary("constructor")}}</li> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/new"><code>new</code> operator</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/not_a_function/index.html b/files/zh-cn/web/javascript/reference/errors/not_a_function/index.html new file mode 100644 index 0000000000..fc4f664e1d --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/not_a_function/index.html @@ -0,0 +1,167 @@ +--- +title: 'TypeError: "x" is not a function' +slug: Web/JavaScript/Reference/Errors/Not_a_function +tags: + - Errors + - JavaScript + - TypeError +translation_of: Web/JavaScript/Reference/Errors/Not_a_function +--- +<div>{{jsSidebar("Errors")}}</div> + +<div>JavaScript异常"is not a function"会在试图去调用一个像函数一样的值,但是该值实际上不是函数时被抛出.</div> + +<h2 id="信息">信息</h2> + +<pre class="notranslate">TypeError: Object doesn't support property or method {x} (Edge) +TypeError: "x" is not a function</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("TypeError")}}</p> + +<h2 id="哪里出错了">哪里出错了?</h2> + +<p>问题出在你试图去调用一个像函数一样的值,但是该值实际上不是函数,有时候你的代码需要调用一些函数,但是那种值并不能当作函数来被调用。</p> + +<p>也许函数名称上有错别字? 也许你正在调用Object对象没有这个方法? 例如,在JavaScript中单纯的对象(Object)没有<strong>map</strong>函数,但是JavaScript数组(Array)对象却有这个函数。</p> + +<p>再比如,在JavaScript中很多原生对象的内置方法需要你提供一个(回调)函数。 所以你必须提供一个函数,以使这些方法正常工作:</p> + +<ul> + <li>当你在使用 {{jsxref("Array")}} 或 {{jsxref("TypedArray")}} 对象时: + <ul> + <li>{{jsxref("Array.prototype.every()")}}, {{jsxref("Array.prototype.some()")}}, {{jsxref("Array.prototype.forEach()")}}, {{jsxref("Array.prototype.map()")}}, {{jsxref("Array.prototype.filter()")}}, {{jsxref("Array.prototype.reduce()")}}, {{jsxref("Array.prototype.reduceRight()")}}, {{jsxref("Array.prototype.find()")}}</li> + </ul> + </li> + <li>当你在使用 {{jsxref("Map")}} 和 {{jsxref("Set")}} 对象时: + <ul> + <li>{{jsxref("Map.prototype.forEach()")}} and {{jsxref("Set.prototype.forEach()")}}</li> + </ul> + </li> +</ul> + +<h2 id="例子">例子</h2> + +<h3 id="函数的名称错误">函数的名称错误</h3> + +<p>函数的名称拼写错误,这种情况是经常发生的:</p> + +<pre class="brush: js example-bad notranslate">var x = document.getElementByID("foo"); +// TypeError: document.getElementByID is not a function +</pre> + +<p>正确的方法名应该是 <code>getElementByI<strong>d:</strong></code></p> + +<pre class="brush: js example-good notranslate">var x = document.getElementById("foo"); +</pre> + +<h3 id="调用Object类型中不存在的方法">调用Object类型中不存在的方法</h3> + +<p>对于某些特殊的方法,它只属于某些特定的原生对象中,你必须提供一个回调函数才能正常运行。例如:这里调用了一个 {{jsxref("Array.prototype.map()")}} 方法,但是这方法只能被 {{jsxref("Array")}} 对象所调用。 </p> + +<pre class="brush: js example-bad notranslate">var obj = { a: 13, b: 37, c: 42 }; + +obj.map(function(num) { + return num * 2; +}); + +// TypeError: obj.map is not a function</pre> + +<p>正确的做法,使用一个数组来代替:</p> + +<pre class="brush: js example-good notranslate">var numbers = [1, 4, 9]; + +numbers.map(function(num) { + return num * 2; +}); + +// Array [ 2, 8, 18 ] +</pre> + +<h3 id="函数与已有属性重名">函数与已有属性重名</h3> + +<p>当您在创建类时,可能会存在某个属性和某个方法的名称相同,当您在调用该函数时,编译器会认为该函数不存在.</p> + +<pre class="brush: js example-bad notranslate">var Dog = function () { + this.age = 11; + this.color = "black"; + this.name = "Ralph"; + return this; +} + +Dog.prototype.name = function(name) { + this.name = name; + return this; +} + + +var myNewDog = new Dog(); +myNewDog.name("Cassidy"); //Uncaught TypeError: myNewDog.name is not a function +</pre> + +<p>正确的做法是使用不同的变量名.</p> + +<pre class="brush: js example-good notranslate">var Dog = function () { + this.age = 11; + this.color = "black"; + this.dogName = "Ralph"; //Using this.dogName instead of .name + return this; +} + +Dog.prototype.name = function(name) { + this.dogName = name; + return this; +} + + +var myNewDog = new Dog(); +myNewDog.name("Cassidy"); //Dog { age: 11, color: 'black', dogName: 'Cassidy' }</pre> + +<h3 id="使用括号进行乘法运算">使用括号进行乘法运算</h3> + +<p>在数学中,您可以将 2 × (3 + 5) 写为 2*(3 + 5) 或者省略为 2(3 + 5).</p> + +<p>使用后者时将会抛出错误:</p> + +<pre class="brush: js example-bad notranslate">const sixteen = 2(3 + 5); +alert('2 x (3 + 5) is ' + String(sixteen)); +//Uncaught TypeError: 2 is not a function</pre> + +<p>您可以添加乘法运算符 <code>*</code> 来改正代码:</p> + +<pre class="brush: js example-good notranslate">const sixteen = 2 * (3 + 5); +alert('2 x (3 + 5) is ' + String(sixteen)); +//2 x (3 + 5) is 16</pre> + +<h3 id="正确地导入和导出模块">正确地导入和导出模块</h3> + +<p>确保正确导入模块.</p> + +<p>以下为一个示例模块 (<code>helpers.js</code>)</p> + +<pre class="notranslate">let helpers = function () { }; + +helpers.groupBy = function (objectArray, property) { + return objectArray.reduce(function (acc, obj) { + var key = obj[property]; + if (!acc[key]) { + acc[key] = []; + } + acc[key].push(obj); + return acc; + }, +{}); +} + +export default helpers;</pre> + +<p>在 <code>App.js</code>中正确导入该模块:</p> + +<pre class="notranslate">import helpers from './helpers'</pre> + +<h2 id="相关">相关</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Functions">Functions</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/not_defined/index.html b/files/zh-cn/web/javascript/reference/errors/not_defined/index.html new file mode 100644 index 0000000000..a092f394ec --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/not_defined/index.html @@ -0,0 +1,66 @@ +--- +title: 'ReferenceError: "x" is not defined' +slug: Web/JavaScript/Reference/Errors/Not_defined +translation_of: Web/JavaScript/Reference/Errors/Not_defined +--- +<p>{{jsSidebar("Errors")}}</p> + +<h2 id="错误信息">错误信息</h2> + +<pre class="syntaxbox notranslate">ReferenceError: "x" is not defined +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("ReferenceError")}}.</p> + +<h2 id="什么地方出错了">什么地方出错了?</h2> + +<p>在某些地方引用一个不存在的变量的时候。当你使用变量的时候,这个变量必须是已经被声明的,或者你可以确保它在你当前的脚本或作用域 ({{Glossary("scope")}}) 中可用。</p> + +<div class="note"> +<p><strong>注意:</strong> 当你加载一个库的时候(例如 jQuery),请确保你在这个库加载完毕后再使用这个库中的变量,如“$”。将你想加载的库的 {{HTMLElement("script")}} 标签放置在你的代码前面。</p> +</div> + +<h2 id="示例">示例</h2> + +<h3 id="变量没有被声明">变量没有被声明</h3> + +<pre class="brush: js example-bad notranslate">foo.substring(1); // ReferenceError: foo is not defined +</pre> + +<p>“foo” 变量没有在任何地方被声明。它需要是某种字符串,这样 {{jsxref("String.prototype.substring()")}} 方法才可以正常工作。</p> + +<pre class="brush: js example-good notranslate">var foo = 'bar'; +foo.substring(1); // "ar"</pre> + +<h3 id="错误的作用域">错误的作用域</h3> + +<p>变量必须是在它当前的执行环境中可用的。在一个函数(<a href="/en-US/docs/Web/JavaScript/Reference/Functions">function</a>)中定义的变量不能从这个函数外部的任何地方访问,因为这个变量的作用域仅在这个函数的内部。</p> + +<pre class="brush: js example-bad notranslate">function numbers () { + var num1 = 2, + num2 = 3; + return num1 + num2; +} + +console.log(num1); // ReferenceError num1 is not defined.</pre> + +<p>然而,一个函数可用使用在它所被定义的作用域中的所有变量。换句话说,当一个函数被定义在全局作用域的时候,它可以访问所有在全局作用域中定义的变量。</p> + +<pre class="brush: js example-good notranslate">var num1 = 2, + num2 = 3; + +function numbers () { + return num1 + num2; +} + +console.log(num1); // 2</pre> + +<h2 id="相关页面">相关页面</h2> + +<ul> + <li>{{Glossary("Scope")}}</li> + <li><a href="/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#Declaring_variables">Declaring variables in the JavaScript Guide</a></li> + <li><a href="/en-US/docs/Web/JavaScript/Guide/Functions#Function_scope/en-US/docs/">Function scope in the JavaScript Guide</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/precision_range/index.html b/files/zh-cn/web/javascript/reference/errors/precision_range/index.html new file mode 100644 index 0000000000..42e2c703a9 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/precision_range/index.html @@ -0,0 +1,96 @@ +--- +title: 'RangeError: precision is out of range' +slug: Web/JavaScript/Reference/Errors/Precision_range +tags: + - JavaScript + - 范围错误 + - 错误 +translation_of: Web/JavaScript/Reference/Errors/Precision_range +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="描述">描述</h2> + +<pre class="syntaxbox">RangeError: precision {0} out of range (Firefox) +RangeError: toExponential() argument must be between 0 and 20 (Chrome) +RangeError: toFixed() digits argument must be between 0 and 20 (Chrome) +RangeError: toPrecision() argument must be between 1 and 21 (Chrome) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("RangeError")}}</p> + +<h2 id="什么地方出错了?">什么地方出错了?</h2> + +<p>以下的某个方法传入了一个超出精度范围的参数:</p> + +<ul> + <li>{{jsxref("Number.prototype.toExponential()")}}</li> + <li>{{jsxref("Number.prototype.toFixed()")}}</li> + <li>{{jsxref("Number.prototype.toPrecision()")}}</li> +</ul> + +<p>通常这些方法允许的参数范围介于0和20(或21)之间。需要注意的是,ECMAScript标准是允许扩展这个范围的。</p> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Method</th> + <th scope="col">Firefox (SpiderMonkey)</th> + <th scope="col">Chrome, Opera (V8)</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{jsxref("Number.prototype.toExponential()")}}</td> + <td>0 to 100</td> + <td>0 to 20</td> + </tr> + <tr> + <td>{{jsxref("Number.prototype.toFixed()")}}</td> + <td>-20 to 100</td> + <td>0 to 20</td> + </tr> + <tr> + <td>{{jsxref("Number.prototype.toPrecision()")}}</td> + <td>1 to 100</td> + <td>1 to 21</td> + </tr> + </tbody> +</table> + +<h2 id="示例">示例</h2> + +<h3 id="错误的示例">错误的示例</h3> + +<pre class="brush: js example-bad">77.1234.toExponential(-1); // RangeError +77.1234.toExponential(101); // RangeError + +2.34.toFixed(-100); // RangeError +2.34.toFixed(1001); // RangeError + +1234.5.toPrecision(-1); // RangeError +1234.5.toPrecision(101); // RangeError +</pre> + +<h3 id="正确的示例">正确的示例</h3> + +<pre class="brush: js example-good">77.1234.toExponential(4); // 7.7123e+1 +77.1234.toExponential(2); // 7.71e+1 + +2.34.toFixed(1); // 2.3 +2.35.toFixed(1); // 2.4 (note that it rounds up in this case) + +5.123456.toPrecision(5); // 5.1235 +5.123456.toPrecision(2); // 5.1 +5.123456.toPrecision(1); // 5 +</pre> + +<h2 id="相关页面">相关页面</h2> + +<ul> + <li>{{jsxref("Number.prototype.toExponential()")}}</li> + <li>{{jsxref("Number.prototype.toFixed()")}}</li> + <li>{{jsxref("Number.prototype.toPrecision()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/property_access_denied/index.html b/files/zh-cn/web/javascript/reference/errors/property_access_denied/index.html new file mode 100644 index 0000000000..7b90b8be6f --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/property_access_denied/index.html @@ -0,0 +1,45 @@ +--- +title: 'Error: Permission denied to access property "x"' +slug: Web/JavaScript/Reference/Errors/Property_access_denied +tags: + - Error + - Permission denied + - 错误 +translation_of: Web/JavaScript/Reference/Errors/Property_access_denied +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="消息">消息</h2> + +<pre class="syntaxbox">Error: Permission denied to access property "x" +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("Error","错误")}}.</p> + +<h2 id="什么地方出错了">什么地方出错了?</h2> + +<p>尝试访问无权访问的对象。这很可能出现在使用{{HTMLElement("iframe")}}元素时加载了一个不同域名下的页面,这在访问子页面时会违背<a href="/zh-CN/docs/Web/Security/Same-origin_policy">同源策略</a>。</p> + +<h2 id="示例">示例</h2> + +<pre class="brush: html"><!DOCTYPE html> +<html> + <head> + <iframe id="myframe" src="http://www1.w3c-test.org/common/blank.html"></iframe> + <script> + console.log(document.getElementById('myframe').contentWindow.document); + // Error: Permission denied to access property "document" + </script> + </head> + <body></body> +</html> +</pre> + +<p>可以参考</p> + +<ul> + <li>{{HTMLElement("iframe")}}</li> + <li><a href="/zh-CN/docs/Web/Security/Same-origin_policy">同源策略</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/read-only/index.html b/files/zh-cn/web/javascript/reference/errors/read-only/index.html new file mode 100644 index 0000000000..ecdc225497 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/read-only/index.html @@ -0,0 +1,76 @@ +--- +title: 'TypeError: "x" is read-only' +slug: Web/JavaScript/Reference/Errors/Read-only +translation_of: Web/JavaScript/Reference/Errors/Read-only +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="报错消息">报错消息</h2> + +<pre class="syntaxbox">TypeError: "x" is read-only (Firefox) //格式错误:"x"只读。(x可以代表任意值) +TypeError: 0 is read-only (Firefox) +TypeError: Cannot assign to read only property 'x' of #<Object> (Chrome) +//格式错误:对象的x属性是只读的不能设置 (chrome) +TypeError: Cannot assign to read only property '0' of [object Array] (Chrome) +</pre> + +<h2 id="错误格式">错误格式</h2> + +<p>{{jsxref("TypeError")}}</p> + +<h2 id="哪里出错了">哪里出错了?</h2> + +<p>全局变量或对象属性被设置为只读 (专业点讲,就是这条数据属性禁止写入.)</p> + +<p>这条错误值发生在<a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">strict mode code</a>(俗称严格模式). 正常情况下,是没有报错的。</p> + +<h2 id="例如">例如</h2> + +<h3 id="无效例子(也就是下面这么做会导致报这种错)">无效例子(也就是下面这么做会导致报这种错)</h3> + +<p>只读属性不能直接创建, 但我们可以通过{{jsxref("Object.defineProperty()")}} 或 {{jsxref("Object.freeze()")}}设置.</p> + +<pre class="brush: js example-bad">"use strict"; +var obj = Object.freeze({name: "Elsa", score: 157}); +obj.score = 0; // TypeError + +"use strict"; +Object.defineProperty(this, "LUNG_COUNT", {value: 2, writable: false}); +LUNG_COUNT = 3; // TypeError + +"use strict"; +var frozenArray = Object.freeze([0, 1, 2]); +frozenArray[0]++; // TypeError + +还有几个JavaScript内置属性. 如果你尝试修改一个常量. + +"use strict"; +Math.PI = 4; // TypeError</pre> + +<p>傻了吧,报错了</p> + +<p><code>全局变量undefined也是只读的</code>, 所以你不能忽视臭名昭著的"undefined is not a function"错误:</p> + +<pre class="brush: js example-bad">"use strict"; +undefined = function () {}; // TypeError: "undefined" is read-only +</pre> + +<h3 id="下面这样都是有效,不报错的">下面这样都是有效,不报错的</h3> + +<pre class="brush: js example-good">"use strict"; +var obj = Object.freeze({name: "Score", points: 157}); +obj = {name: obj.name, points: 0}; // 用一个新对象替换原来的对象(其实就是更改了对象的指针) + +"use strict"; +var LUNG_COUNT = 2; // +LUNG_COUNT = 3; // +</pre> + +<h2 id="参见">参见</h2> + +<ul> + <li>{{jsxref("Object.defineProperty()")}}</li> + <li>{{jsxref("Object.freeze()")}}</li> + <li><a href="https://www.answers.com/Q/Which_animals_have_three_lungs">"Which animals have three lungs?" on answers.com</a></li> + <li><a href="https://aliens.wikia.com/wiki/Klingon">Klingons</a> (another answer to that query)</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/redeclared_parameter/index.html b/files/zh-cn/web/javascript/reference/errors/redeclared_parameter/index.html new file mode 100644 index 0000000000..9a53795180 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/redeclared_parameter/index.html @@ -0,0 +1,62 @@ +--- +title: 'SyntaxError: redeclaration of formal parameter "x"' +slug: Web/JavaScript/Reference/Errors/Redeclared_parameter +tags: + - Errors + - JavaScript + - SyntaxError +translation_of: Web/JavaScript/Reference/Errors/Redeclared_parameter +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="信息">信息</h2> + +<pre class="syntaxbox">SyntaxError: redeclaration of formal parameter "x" (Firefox) +SyntaxError: Identifier "x" has already been declared (Chrome) +SyntaxError: Cannot declare a let variable twice: 'x' (WebKit) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("SyntaxError")}}</p> + +<h2 id="哪里出错了">哪里出错了?</h2> + +<p>某个变量名称已经作为函数参数出现了,但是又使用了 <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/let">let</a></code> 在函数体里重声明了。在JavaScript 中不允许使用let在相同的函数或块范围内重新声明相同的变量。</p> + +<h2 id="示例">示例</h2> + +<p>在本例中,参数 "arg" 又重新声明了:</p> + +<pre class="brush: js example-bad">function f(arg) { + let arg = "foo"; +} + +// SyntaxError: redeclaration of formal parameter "arg" +</pre> + +<p>如果要更改函数体中的“arg”的值,可以像下面一样,但不需要再次声明同一个变量。 换句话说:你可以省略 let 关键字。 如果要创建一个新变量,则需要将其重命名,因为其与函数参数有冲突。</p> + +<pre class="brush: js example-good">function f(arg) { + arg = "foo"; +} + +function f(arg) { + let bar = "foo"; +} +</pre> + +<h2 id="兼容性提醒">兼容性提醒</h2> + +<ul> + <li>在 Firefox 49 {{geckoRelease(49)}} 之前,会抛出的是 {{jsxref("TypeError")}} ({{bug(1275240)}})。</li> +</ul> + +<h2 id="相关">相关</h2> + +<ul> + <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/let">let</a></code></li> + <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/const">const</a></code></li> + <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/var">var</a></code></li> + <li><a href="/en-US/docs/Web/JavaScript/Guide/Grammar_and_Types#Declarations">Declaring variables</a> in the <a href="/en-US/docs/Web/JavaScript/Guide">JavaScript Guide</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/reduce_of_empty_array_with_no_initial_value/index.html b/files/zh-cn/web/javascript/reference/errors/reduce_of_empty_array_with_no_initial_value/index.html new file mode 100644 index 0000000000..3d90929530 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/reduce_of_empty_array_with_no_initial_value/index.html @@ -0,0 +1,88 @@ +--- +title: 'TypeError: Reduce of empty array with no initial value' +slug: Web/JavaScript/Reference/Errors/Reduce_of_empty_array_with_no_initial_value +tags: + - Error + - JavaScript + - 参考 + - 错误 +translation_of: Web/JavaScript/Reference/Errors/Reduce_of_empty_array_with_no_initial_value +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="消息">消息</h2> + +<pre class="syntaxbox">TypeError: reduce of empty array with no initial value +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("TypeError")}}</p> + +<h2 id="哪里出问题了?">哪里出问题了?</h2> + +<p>在 JavaScript 中,有一些 reduce 函数:</p> + +<ul> + <li>{{jsxref("Array.prototype.reduce()")}},{{jsxref("Array.prototype.reduceRight()")}} 和</li> + <li>{{jsxref("TypedArray.prototype.reduce()")}}, {{jsxref("TypedArray.prototype.reduceRight()")}})。</li> +</ul> + +<p>这些函数有选择性的接收 <code>initialValue</code> (将会被用作回调函数第一次被调用的参数)。然而,如果没有初始值提供,它会用 {{jsxref("Array")}} 或 {{jsxref("TypedArray")}} 的第一个元素作为初始值。当提供一个空数组的时候这个错误会出现,因为那种情况下没有初始值被返回。</p> + +<h2 id="例子">例子</h2> + +<h3 id="无效的情况">无效的情况</h3> + +<p>当和过滤器({{jsxref("Array.prototype.filter()")}}、{{jsxref("TypedArray.prototype.filter()")}})结合使用的时候,可能会出现列表中所有元素被移除的情况。因此导致初始值为空。</p> + +<pre class="brush: js example-bad">var ints = [0, -1, -2, -3, -4, -5]; +ints.filter(x => x > 0) // removes all elements + .reduce((x, y) => x + y) // no more elements to use for the initial value.</pre> + +<p>类似的,当选择器中有瑕疵的时候相同的问题会发生,或者是列表中未预期的数量的元素:</p> + +<pre class="brush: js example-bad">var names = document.getElementsByClassName("names"); +var name_list = Array.prototype.reduce.call(names, (acc, name) => acc + ", " + name); +</pre> + +<h3 id="有效的情况">有效的情况</h3> + +<p>这个问题有两种可能的解决办法:</p> + +<p>一种是提供一个初始值作为操作符的中立元素,比如加法里的0,乘法里的1,或者是合并中的一个空字符串。</p> + +<pre class="brush: js example-good">var ints = [0, -1, -2, -3, -4, -5]; +ints.filter(x => x < 0) // removes all elements + .reduce((x, y) => x + y, 0) // the initial value is the neutral element of the addition +</pre> + +<p>另一种办法是两方处理空的情况,要么在调用 <code>reduce</code> 之前,或者是在添加一个未预料的初始虚拟址后的回调函数中:</p> + +<pre class="brush: js example-good">var names = document.getElementsByClassName("names"); + +var name_list1 = ""; +if (names1.length >= 1) + name_list1 = Array.prototype.reduce.call(names, (acc, name) => acc + ", " + name); +// name_list1 == "" when names is empty. + +var name_list2 = Array.prototype.reduce.call(names, (acc, name) => { + if (acc == "") // initial value + return name; + return acc + ", " + name; +}, ""); +// name_list2 == "" when names is empty. +</pre> + +<h2 id="另见">另见</h2> + +<ul> + <li>{{jsxref("Array.prototype.reduce()")}}</li> + <li>{{jsxref("Array.prototype.reduceRight()")}}</li> + <li>{{jsxref("TypedArray.prototype.reduce()")}}</li> + <li>{{jsxref("TypedArray.prototype.reduceRight()")}}</li> + <li>{{jsxref("Array")}}</li> + <li>{{jsxref("TypedArray")}}</li> + <li>{{jsxref("Array.prototype.filter()")}}</li> + <li>{{jsxref("TypedArray.prototype.filter()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/reserved_identifier/index.html b/files/zh-cn/web/javascript/reference/errors/reserved_identifier/index.html new file mode 100644 index 0000000000..d99acf3fa8 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/reserved_identifier/index.html @@ -0,0 +1,79 @@ +--- +title: 'SyntaxError: "x" is a reserved identifier' +slug: Web/JavaScript/Reference/Errors/Reserved_identifier +tags: + - JavaScript + - 语法错误 + - 错误 +translation_of: Web/JavaScript/Reference/Errors/Reserved_identifier +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="消息提示">消息提示</h2> + +<pre class="syntaxbox">SyntaxError: "x" is a reserved identifier (Firefox) +SyntaxError: Unexpected reserved word (Chrome)</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("SyntaxError")}}</p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p><a href="/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords">保留字</a> 用作标记符将会出错. 这些标记符在严格模式和非严格模式下保留:</p> + +<ul> + <li><code>enum</code></li> +</ul> + +<p>以下标记符只会在严格模式下才作为保留字:</p> + +<ul class="threecolumns"> + <li><code>implements</code></li> + <li><code>interface</code></li> + <li>{{jsxref("Statements/let", "let")}}</li> + <li><code>package</code></li> + <li><code>private</code></li> + <li><code>protected</code></li> + <li><code>public</code></li> + <li><code>static</code></li> +</ul> + +<h2 id="示例">示例</h2> + +<h3 id="严格与非严格模式下的保留字">严格与非严格模式下的保留字</h3> + +<p>在两种模式下,<code>enum</code> 标识符都会作为保留字。</p> + +<pre class="brush: js example-bad">var enum = { RED: 0, GREEN: 1, BLUE: 2 }; +// SyntaxError: enum is a reserved identifier +</pre> + +<p>在严格模式下,会有更多的保留字。</p> + +<pre class="brush: js example-bad">"use strict"; +var package = ["potatoes", "rice", "fries"]; +// SyntaxError: package is a reserved identifier +</pre> + +<p>你需要对上述变量重新命名。</p> + +<pre class="brush: js example-good">var colorEnum = { RED: 0, GREEN: 1, BLUE: 2 }; +var list = ["potatoes", "rice", "fries"];</pre> + +<h3 id="升级旧版本浏览器">升级旧版本浏览器</h3> + +<p>假如你还在使用尚未支持 let 或 class 等特性的旧版本浏览器,你应该将它们升级到支持这些新语言特性的版本。</p> + +<pre class="brush: js">"use strict"; +class DocArchiver {} + +// SyntaxError: class is a reserved identifier +//(只会在旧版本浏览器中抛出,例如 Firefox 44 或更老的版本) +</pre> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li><a href="http://wiki.c2.com/?GoodVariableNames">Good variable names</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/resulting_string_too_large/index.html b/files/zh-cn/web/javascript/reference/errors/resulting_string_too_large/index.html new file mode 100644 index 0000000000..eaac0884fb --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/resulting_string_too_large/index.html @@ -0,0 +1,50 @@ +--- +title: 'RangeError: repeat count must be less than infinity' +slug: Web/JavaScript/Reference/Errors/Resulting_string_too_large +tags: + - Errors + - JavaScript + - RangeError + - 错误 +translation_of: Web/JavaScript/Reference/Errors/Resulting_string_too_large +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="信息">信息</h2> + +<pre class="syntaxbox">RangeError: repeat count must be less than infinity and not overflow maximum string size (Firefox) + +RangeError: Invalid count value (Chrome) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("RangeError")}}</p> + +<h2 id="发生了什么">发生了什么?</h2> + +<p>代码中使用了 {{jsxref("String.prototype.repeat()")}}方法。<span class="short_text" id="result_box" lang="zh-CN"><span>它有一个计数参数,表示重复该字符串的次数</span></span>。该参数必须在 0 及正 {{jsxref("Infinity")}} 之间,且不能为负数。该值的合法范围可以这样表示: [0, +∞)。</p> + +<p><span class="short_text" id="result_box" lang="zh-CN"><span>其结果字符串也不能长于最大字符串,不同 JavaScript 引擎中可能有所不同。</span></span> 在 Firefox (SpiderMonkey) 里最大字符串大小为 2<sup>28</sup> -1 (<code>0xFFFFFFF</code>)。</p> + +<h2 id="示例">示例</h2> + +<h3 id="无效的">无效的</h3> + +<pre class="brush: js example-bad">'abc'.repeat(Infinity); // RangeError +'a'.repeat(2**28); // RangeError +</pre> + +<h3 id="有效的">有效的</h3> + +<pre class="brush: js example-good">'abc'.repeat(0); // '' +'abc'.repeat(1); // 'abc' +'abc'.repeat(2); // 'abcabc' +'abc'.repeat(3.5); // 'abcabcabc' (count will be converted to integer) +</pre> + +<h2 id="See_also">See also</h2> + +<ul> + <li>{{jsxref("String.prototype.repeat()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/stmt_after_return/index.html b/files/zh-cn/web/javascript/reference/errors/stmt_after_return/index.html new file mode 100644 index 0000000000..54fb1cf7a3 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/stmt_after_return/index.html @@ -0,0 +1,76 @@ +--- +title: 'Warning: unreachable code after return statement' +slug: Web/JavaScript/Reference/Errors/Stmt_after_return +tags: + - 警告 +translation_of: Web/JavaScript/Reference/Errors/Stmt_after_return +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="信息">信息</h2> + +<pre class="syntaxbox">Warning: unreachable code after return statement (Firefox) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>警告</p> + +<h2 id="什么地方出错了">什么地方出错了?</h2> + +<p>在<code>return</code>语句之后的不可达的语句会在下列情形中产生:</p> + +<ul> + <li>在{{jsxref("Statements/return", "return")}} 语句之后出现其他表达式;</li> + <li>在省略分号的return语句之后直接接一个表达式。</li> +</ul> + +<p>当一个表达式出现在一个有效的 <code>return </code>表达式之后时,会出现这个警告,用以说明在 <code>return </code>语句之后的表达式不可达,即这条语句之后的表达式永远不会运行。</p> + +<p>为什么我需要在 <code>return </code>语句之后添加分号?在省略分号的 <code>return </code>语句之后,开发者想要终止当前函数的执行还是返回return之后表达式的结果的意图是不明确的。这个警告表明这种情况下 <code>return </code>语句的表述具有二义性。</p> + +<p>当在省略分号的return语句之后出现下列语句时,不会出现警告:</p> + +<ul> + <li>{{jsxref("Statements/throw", "throw")}}</li> + <li>{{jsxref("Statements/break", "break")}}</li> + <li>{{jsxref("Statements/var", "var")}}</li> + <li>{{jsxref("Statements/function", "function")}}</li> +</ul> + +<h2 id="示例">示例</h2> + +<h3 id="无效的例子">无效的例子</h3> + +<pre class="brush: js example-bad">function f() { + var x = 3; + x += 4; + return x; // <code>return </code>语句立即退出当前方法 + x -= 3; // 因而该语句从不会执行,即该语句为不可达语句 +} + +function f() { + return // 这条语句被视作 `return;` + 3 + 4; // 因而此处该函数已经返回,该语句永不会执行 +} +</pre> + +<h3 id="合适的例子">合适的例子</h3> + +<pre class="brush: js example-good">function f() { + var x = 3; + x += 4; + x -= 3; + return x; // OK: 执行完成所有语句之后返回 +} + +function f() { + return 3 + 4 // OK: 省略分号的 <code>return </code>语句与执行的表达式在同一行 +} +</pre> + +<h2 id="相关页面">相关页面</h2> + +<ul> + <li>{{jsxref("Statements/return", "Automatic Semicolon Insertion", "#Automatic_Semicolon_Insertion", 1)}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/strict_non_simple_params/index.html b/files/zh-cn/web/javascript/reference/errors/strict_non_simple_params/index.html new file mode 100644 index 0000000000..3059705d88 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/strict_non_simple_params/index.html @@ -0,0 +1,113 @@ +--- +title: 'SyntaxError: "use strict" not allowed in function with non-simple parameters' +slug: Web/JavaScript/Reference/Errors/Strict_Non_Simple_Params +tags: + - Errors + - JavaScript + - SyntaxError + - TypeError + - use strict +translation_of: Web/JavaScript/Reference/Errors/Strict_Non_Simple_Params +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="信息">信息</h2> + +<pre class="syntaxbox">Firefox: +句法错误: "use strict" 不允许在带默认参数的函数中 +句法错误: "use strict" 不允许在带rest参数的函数中 +句法错误: "use strict" 不允许在带解构参数的函数中 + +Chrome: +句法错误: 非法的'use strict'指令,在带有非简单参数列表的函数中 +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("SyntaxError")}}.</p> + +<h2 id="哪里出错了">哪里出错了?</h2> + +<p><code><font face="Open Sans, Arial, sans-serif">在函数顶部直接写了 </font>"use strict"</code> ,而该函数拥有以下的参数其中之一:</p> + +<ul> + <li>{{jsxref("Functions/Default_parameters", "默认参数", "", 1)}}</li> + <li>{{jsxref("Functions/rest_parameters", "剩余参数", "", 1)}}</li> + <li>{{jsxref("Operators/Destructuring_assignment", "解构赋值", "", 1)}}</li> +</ul> + +<p>根据ECMAScript规范,不允许在这些函数的顶部使用“use strict”指令。</p> + +<h2 id="示例">示例</h2> + +<h3 id="函数语句">函数语句</h3> + +<p>在这种情况下,函数sum具有默认参数a = 1和b = 2:</p> + +<pre class="brush: js example-bad">function sum(a=1, b=2) { + // SyntaxError: "use strict" not allowed in function with default parameter + "use strict"; + return a + b; +} +</pre> + +<p>如果这个函数应该处于 <a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">strict mode</a>,并且整个脚本或封装函数也可以在严格模式下,可以移动 "use strict" 指令到函数之外:</p> + +<pre class="brush: js example-good">"use strict"; +function sum(a=1, b=2) { + return a + b; +} +</pre> + +<h3 id="函数表达式">函数表达式</h3> + +<p>函数表达式可以使用另一种解决方法:</p> + +<pre class="brush: js example-bad">var sum = function sum([a, b]) { + // SyntaxError: "use strict" not allowed in function with destructuring parameter + "use strict"; + return a + b; +}; +</pre> + +<p>这可以转换为以下表达式:</p> + +<pre class="brush: js example-good">var sum = (function() { + "use strict"; + return function sum([a, b]) { + return a + b; + }; +})(); +</pre> + +<h3 id="箭头函数">箭头函数</h3> + +<p>如果箭头函数需要访问 <code>this</code>,则可以将箭头函数作为封闭函数来使用:</p> + +<pre class="brush: js example-bad">var callback = (...args) => { + // SyntaxError: "use strict" not allowed in function with rest parameter + "use strict"; + return this.run(args); +}; +</pre> + +<p>这可以转换为以下表达式:</p> + +<pre class="brush: js example-good">var callback = (() => { + "use strict"; + return (...args) => { + return this.run(args); + }; +})(); +</pre> + +<h2 id="也可以看看">也可以看看</h2> + +<ul> + <li>{{jsxref("Strict_mode", "Strict mode", "", 1)}}</li> + <li>{{jsxref("Statements/function", "函数语句", "", 1)}}</li> + <li>{{jsxref("Operators/function", "函数表达式", "", 1)}}</li> + <li>{{jsxref("Functions/Default_parameters", "默认参数", "", 1)}}</li> + <li>{{jsxref("Functions/rest_parameters", "剩余参数", "", 1)}}</li> + <li>{{jsxref("Operators/Destructuring_assignment", "解构参数", "", 1)}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/too_much_recursion/index.html b/files/zh-cn/web/javascript/reference/errors/too_much_recursion/index.html new file mode 100644 index 0000000000..1671a5598a --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/too_much_recursion/index.html @@ -0,0 +1,54 @@ +--- +title: 'InternalError: too much recursion' +slug: Web/JavaScript/Reference/Errors/Too_much_recursion +tags: + - InternalError + - recursion + - 内部错误 +translation_of: Web/JavaScript/Reference/Errors/Too_much_recursion +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="信息">信息</h2> + +<pre class="syntaxbox">InternalError: too much recursion +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("InternalError","内部错误")}}.</p> + +<h2 id="什么地方出错了">什么地方出错了?</h2> + +<p>一个调用自身的函数被称作<em>递归函数</em>。一些情况下,递归函数类似于一个循环,都重复地执行一个代码段许多次,都需要一个条件(用于避免无尽循环或此处的无尽递归)。当出现过于深层的递归或无尽递归时,JavaScript将会抛出此错误。</p> + +<h2 id="示例">示例</h2> + +<p>根据递归终止的条件,该函数将递归地执行 10 次。</p> + +<pre class="brush: js">function loop(x) { + if (x >= 10) // "x >= 10" 是递归终止条件 + return; + // 进行一些操作... + loop(x + 1); // 递归调用 +} +loop(0);</pre> + +<p>将递归条件设置为一个极大的数值,将不能运行:</p> + +<pre class="brush: js example-bad">function loop(x) { + if (x >= 1000000000000) + return; + // 进行一些操作... + loop(x + 1); +} +loop(0); + +// InternalError: too much recursion</pre> + +<h2 id="相关页面">相关页面</h2> + +<ul> + <li>{{Glossary("Recursion","递归")}}</li> + <li><a href="/en-US/docs/Web/JavaScript/Guide/Functions#Recursion">Recursive functions</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/typed_array_invalid_arguments/index.html b/files/zh-cn/web/javascript/reference/errors/typed_array_invalid_arguments/index.html new file mode 100644 index 0000000000..314a4a425c --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/typed_array_invalid_arguments/index.html @@ -0,0 +1,76 @@ +--- +title: 'TypeError: invalid arguments' +slug: Web/JavaScript/Reference/Errors/Typed_array_invalid_arguments +tags: + - JavaScript + - 类型错误 + - 错误提示 +translation_of: Web/JavaScript/Reference/Errors/Typed_array_invalid_arguments +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="错误提示">错误提示</h2> + +<pre class="syntaxbox">TypeError: invalid arguments (Firefox)</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("TypeError")}}</p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p><a href="/en-US/docs/Web/JavaScript/Typed_arrays">类型化数组(Typed array</a>) 的构造器可以通过接收以下类型的参数中的一种</p> + +<ul> + <li>表示长度的数字,</li> + <li>另外一个类型化数组,</li> + <li>类数组对象,</li> + <li>可迭代对象,</li> + <li>一个 {{jsxref("ArrayBuffer")}} 对象</li> +</ul> + +<p>来创建一个新的类型化数组。其他类型的构造器参数都无法创建合法的类型化数组。</p> + +<h2 id="示例">示例</h2> + +<p>类型化数组——例如 {{jsxref("Uint8Array")}} ——无法通过字符串创建。实际上,字符串根本不能出现在类型化数组中。</p> + +<pre class="brush: js example-bad">var ta = new Uint8Array("nope"); +// TypeError: invalid arguments +</pre> + +<p>创建一个合法的 {{jsxref("Uint8Array")}} 对象的不同方式:</p> + +<pre class="brush: js example-good">// From a length +var uint8 = new Uint8Array(2); +uint8[0] = 42; +console.log(uint8[0]); // 42 +console.log(uint8.length); // 2 +console.log(uint8.BYTES_PER_ELEMENT); // 1 + +// From an array +var arr = new Uint8Array([21,31]); +console.log(arr[1]); // 31 + +// From another TypedArray +var x = new Uint8Array([21, 31]); +var y = new Uint8Array(x); +console.log(y[0]); // 21 + +// From an ArrayBuffer +var buffer = new ArrayBuffer(8); +var z = new Uint8Array(buffer, 1, 4); + +// From an iterable +var iterable = function*(){ yield* [1,2,3]; }(); +var uint8 = new Uint8Array(iterable); +// Uint8Array[1, 2, 3] +</pre> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Typed_arrays">Typed arrays</a></li> + <li>{{jsxref("ArrayBuffer")}}</li> + <li>{{jsxref("Uint8Array")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/undeclared_var/index.html b/files/zh-cn/web/javascript/reference/errors/undeclared_var/index.html new file mode 100644 index 0000000000..507da98563 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/undeclared_var/index.html @@ -0,0 +1,68 @@ +--- +title: 'ReferenceError: assignment to undeclared variable "x"' +slug: Web/JavaScript/Reference/Errors/Undeclared_var +tags: + - Errors + - JavaScript + - ReferenceError + - 严格模式 + - 错误 +translation_of: Web/JavaScript/Reference/Errors/Undeclared_var +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="信息">信息</h2> + +<pre class="syntaxbox">ReferenceError: assignment to undeclared variable "x" (Firefox) +ReferenceError: "x" is not defined (Chrome) +ReferenceError: Variable undefined in strict mode (Edge) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p> 仅在<a href="/zh-CN/docs/Web/JavaScript/Reference/Strict_mode">严格模式</a>中出现 {{jsxref("ReferenceError")}} 警告。</p> + +<h2 id="发生了什么">发生了什么?</h2> + +<p>在代码里赋值了一个未声明的变量。换句话说,有处没有带着<code><a href="/zh-CN/docs/Web/JavaScript/Reference/Statements/var"> var</a></code> 关键字的赋值。<span id="result_box" lang="zh-CN"><span>事实上已声明的和未声明的变量之间有一些差异,这可能会导致意想不到的结果,这就是为什么 JavaScript 在严格模式打印出这种错误。</span></span></p> + +<p><span class="short_text" id="result_box" lang="zh-CN"><span>关于已声明和未声明的变量,其有三个注意事项:</span></span></p> + +<ul> + <li><span class="short_text" id="result_box" lang="zh-CN"><span>已声明的变量被约束在其执行上下文中</span></span><span class="short_text" lang="zh-CN"><span>。</span><span>未声明的变量总是全局的。</span></span></li> + <li><span class="short_text" id="result_box" lang="zh-CN"><span>已声明的变量在执行任何代码之前就创建了</span></span><span class="short_text" lang="zh-CN"><span>。未声明的变量则不存在,直到执行相关的代码。</span></span></li> + <li><span class="short_text" id="result_box" lang="zh-CN"><span>已声明的变量是其执行上下文(函数或全局)的不可配置属性。</span><span>而未声明的变量是可配置的(例如可以删除)。</span></span></li> +</ul> + +<p>更多信息及例子,请参考 <code><a href="/zh-CN/docs/Web/JavaScript/Reference/Statements/var">var</a></code> 页面。</p> + +<p>关于未声明变量的赋值的错误仅在<a href="/zh-CN/docs/Web/JavaScript/Reference/Strict_mode">严格模式</a>里出现。在非严格模式中,这些将被忽略。</p> + +<h2 id="示例">示例</h2> + +<h3 id="无效的">无效的</h3> + +<p>在本例中,"bar" 是未声明的变量。</p> + +<pre class="brush: js example-bad">function foo() { + "use strict"; + bar = true; +} +foo(); // ReferenceError: assignment to undeclared variable bar +</pre> + +<h3 id="有效的">有效的</h3> + +<p>为了使 "bar" 是一个已声明变量,你需要在其前面加一个 var 关键字。</p> + +<pre class="brush: js example-good">function foo() { + "use strict"; + var bar = true; +} +foo();</pre> + +<h2 id="相关">相关</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">Strict mode</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/undefined_prop/index.html b/files/zh-cn/web/javascript/reference/errors/undefined_prop/index.html new file mode 100644 index 0000000000..9cda2d0501 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/undefined_prop/index.html @@ -0,0 +1,64 @@ +--- +title: 'ReferenceError: reference to undefined property "x"' +slug: Web/JavaScript/Reference/Errors/Undefined_prop +tags: + - Errors + - JavaScript + - ReferenceError + - Strict Mode + - 严格模式 +translation_of: Web/JavaScript/Reference/Errors/Undefined_prop +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="信息">信息</h2> + +<pre class="syntaxbox">ReferenceError: reference to undefined property "x" (Firefox) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>仅在 <a href="/zh-CN/docs/Web/JavaScript/Reference/Strict_mode">strict mode</a> 下出现 {{jsxref("ReferenceError")}} 警告。</p> + +<h2 id="哪里出错了">哪里出错了?</h2> + +<p>脚本尝试去访问一个不存在的对象属性。<a href="/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors">property accessors</a> 页面描述了两种访问属性的方法。</p> + +<p>引用未定义属性的错误仅出现在 <a href="/zh-CN/docs/Web/JavaScript/Reference/Strict_mode">strict mode </a>代码中。在非严格代码中,对不存在的属性的访问将被忽略。</p> + +<h2 id="示例">示例</h2> + +<h3 id="无效的">无效的</h3> + +<p>本例中,<code>bar</code> 属性是未定义的,隐藏 <code>ReferenceError</code> 会出现。</p> + +<pre class="brush: js example-bad">"use strict"; + +var foo = {}; +foo.bar; // ReferenceError: reference to undefined property "bar" +</pre> + +<h3 id="无效的_2">无效的</h3> + +<p>为了避免错误,您需要向对象添加 <code>bar</code> 的定义或在尝试访问 <code>bar</code> 属性之前检查 <code>bar</code> 属性的存在;一种检查的方式是使用 {{jsxref("Object.prototype.hasOwnProperty()")}} 方法。如下所示:</p> + +<pre class="brush: js example-good">"use strict"; + +var foo = {}; + +// Define the bar property + +foo.bar = "moon"; +console.log(foo.bar); // "moon" + +// Test to be sure bar exists before accessing it + +if (foo.hasOwnProperty("bar") { + console.log(foo.bar); +}</pre> + +<h2 id="相关">相关</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">Strict mode</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/unexpected_token/index.html b/files/zh-cn/web/javascript/reference/errors/unexpected_token/index.html new file mode 100644 index 0000000000..3de534c36a --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/unexpected_token/index.html @@ -0,0 +1,50 @@ +--- +title: 'SyntaxError: Unexpected token' +slug: Web/JavaScript/Reference/Errors/Unexpected_token +tags: + - Errors + - JavaScript + - SyntaxError +translation_of: Web/JavaScript/Reference/Errors/Unexpected_token +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="信息">信息</h2> + +<pre class="syntaxbox">SyntaxError: expected expression, got "x" +SyntaxError: expected property name, got "x" +SyntaxError: expected target, got "x" +SyntaxError: expected rest argument name, got "x" +SyntaxError: expected closing parenthesis, got "x" +SyntaxError: expected '=>' after argument list, got "x" +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("SyntaxError")}}</p> + +<h2 id="哪里出错了">哪里出错了?</h2> + +<p>期望获得一个特定的语法结构,但得到了其他的。 可能只是一个简单的错字。</p> + +<h2 id="示例">示例</h2> + +<h3 id="期望的表达式">期望的表达式</h3> + +<p>例如,在调用函数时,不允许使用尾随逗号。 有尾逗号的时候,JavaScript 会期望有另一个参数,可以是任何表达式。</p> + +<pre class="brush: js example-bad">Math.max(2, 42,); +// SyntaxError: expected expression, got ')' +</pre> + +<p>正确的方法是省略最后一个逗号或添加另一个参数:</p> + +<pre class="brush: js example-good">Math.max(2, 42); +Math.max(2, 42, 13+37); +</pre> + +<h2 id="相关">相关</h2> + +<ul> + <li>{{jsxref("Math.max()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/unexpected_type/index.html b/files/zh-cn/web/javascript/reference/errors/unexpected_type/index.html new file mode 100644 index 0000000000..c90cb20c27 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/unexpected_type/index.html @@ -0,0 +1,65 @@ +--- +title: 'TypeError: "x" is (not) "y"' +slug: Web/JavaScript/Reference/Errors/Unexpected_type +tags: + - 类型错误 +translation_of: Web/JavaScript/Reference/Errors/Unexpected_type +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="消息">消息</h2> + +<pre class="syntaxbox">TypeError: "x" is (not) "y" + +Examples: +TypeError: "x" is undefined +TypeError: "x" is null +TypeError: "undefined" is not an object +TypeError: "x" is not an object or null +TypeError: "x" is not a symbol +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("TypeError")}}.</p> + +<h2 id="什么地方出错了">什么地方出错了?</h2> + +<p>出现了与期望不符的类型。 这个错误常常由 {{jsxref("undefined")}} 或 {{jsxref("null")}} 值引起。</p> + +<p>此外,某些方法,例如 {{jsxref("Object.create()")}} 或 {{jsxref("Symbol.keyFor()")}}, 要求必须提供特定类型的参数。</p> + +<h2 id="示例">示例</h2> + +<h3 id="错误情形">错误情形</h3> + +<pre class="brush: js example-bad">// undefined and null cases on which the substring method won't work +var foo = undefined; +foo.substring(1); // TypeError: foo is undefined + +var foo = null; +foo.substring(1); // TypeError: foo is null + + +// Certain methods might require a specific type +var foo = {} +Symbol.keyFor(foo); // TypeError: foo is not a symbol + +var foo = 'bar' +Object.create(foo); // TypeError: "foo" is not an object or null +</pre> + +<h3 id="解决方法">解决方法</h3> + +<p>要解决空指针以及 <code>undefined</code> 或 <code>null</code> 值的问题, 你可以使用 <a href="/en-US/docs/Web/JavaScript/Reference/Operators/typeof">typeof</a> 操作符, 例如:</p> + +<pre class="brush: js">if (typeof foo !== 'undefined') { + // Now we know that foo is defined, we are good to go. +}</pre> + +<h2 id="相关页面">相关页面</h2> + +<ul> + <li>{{jsxref("undefined")}}</li> + <li>{{jsxref("null")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/unnamed_function_statement/index.html b/files/zh-cn/web/javascript/reference/errors/unnamed_function_statement/index.html new file mode 100644 index 0000000000..83d147aaab --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/unnamed_function_statement/index.html @@ -0,0 +1,114 @@ +--- +title: 'SyntaxError: function statement requires a name' +slug: Web/JavaScript/Reference/Errors/Unnamed_function_statement +tags: + - JavaScript + - 语法错误 + - 错误提示 +translation_of: Web/JavaScript/Reference/Errors/Unnamed_function_statement +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="错误提示">错误提示</h2> + +<pre class="syntaxbox">SyntaxError: function statement requires a name [Firefox] +SyntaxError: Unexpected token ( [Chrome] +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("SyntaxError")}}</p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p><a href="/en-US/docs/Web/JavaScript/Reference/Statements/function">函数声明</a>需要提供函数名称。你需要检查函数是如何定义的,是否需要为其提供名称,出现问题的函数是否需要声明为函数表达式或立即调用函数表达式({{Glossary("IIFE")}}),以及函数在上下文环境中出现的位置是否正确。</p> + +<h2 id="示例">示例</h2> + +<h3 id="语句与表达式">语句与表达式</h3> + +<p><a href="/en-US/docs/Web/JavaScript/Reference/Statements/function">函数语句</a>(或函数声明)需要命名,以下写法是不正确的:</p> + +<pre class="brush: js example-bad">function () { + return 'Hello world'; +} +// SyntaxError: function statement requires a name +</pre> + +<p>你可以使用<a href="/en-US/docs/Web/JavaScript/Reference/Operators/function">函数表达式</a>(赋值)来代替:</p> + +<pre class="brush: js example-good">var greet = function() { + return 'Hello world'; +};</pre> + +<p>者是你想将其作为立即调用函数表达式(<a href="https://en.wikipedia.org/wiki/Immediately-invoked_function_expression">IIFE</a>,Immediately Invoked Function Expression),也就是定义后立即执行的函数。在这种情况下你需要用到更多的括号:</p> + +<pre class="brush: js example-good">(function () { + +})();</pre> + +<h3 id="标号函数(Labeled_functions)">标号函数(Labeled functions)</h3> + +<p>你使用函数标号(<a href="/en-US/docs/Web/JavaScript/Reference/Statements/label">labels)</a>的时候, 也需要在关键词 <code>function</code> 后面提供一个函数名称. 这样的代码是不能运行的:</p> + +<pre class="brush: js example-bad">function Greeter() { + german: function () { + return "Moin"; + } +} +// SyntaxError: function statement requires a name +</pre> + +<p>这个例子可以正常运行:</p> + +<pre class="brush: js example-good">function Greeter() { + german: function g() { + return "Moin"; + } +}</pre> + +<h3 id="对象方法">对象方法</h3> + +<p>如果你想创建创建一个对象方法,那么需要首先创建一个对象。以下语法(function 关键字后面没有提供名称)是合法的:</p> + +<pre class="brush: js example-good">var greeter = { + german: function () { + return "Moin"; + } +};</pre> + +<h3 id="回调函数的语法">回调函数的语法</h3> + +<p>另外,如果使用到了回调函数,那么检查一下语法是否正确。大括号与逗号很容易使情况变糟。</p> + +<pre class="brush: js example-bad">promise.then( + function() { + console.log("success"); + }); + function() { + console.log("error"); +} +// SyntaxError: function statement requires a name +</pre> + +<p>正确的形式应该是这样的:</p> + +<pre class="brush: json example-good">promise.then( + function() { + console.log("success"); + }, + function() { + console.log("error"); + } +); +</pre> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Guide/Functions">Functions in the JavaScript Guide</a></li> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Statements/function">function statement</a></li> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/function">function expression</a></li> + <li><a href="https://en.wikipedia.org/wiki/Immediately-invoked_function_expression">IIFE</a></li> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Statements/label">label</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/unterminated_string_literal/index.html b/files/zh-cn/web/javascript/reference/errors/unterminated_string_literal/index.html new file mode 100644 index 0000000000..b0ab684413 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/unterminated_string_literal/index.html @@ -0,0 +1,67 @@ +--- +title: 'SyntaxError: unterminated string literal' +slug: Web/JavaScript/Reference/Errors/Unterminated_string_literal +tags: + - Error + - JavaScript + - SyntaxError +translation_of: Web/JavaScript/Reference/Errors/Unterminated_string_literal +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="信息">信息</h2> + +<pre class="syntaxbox">SyntaxError: unterminated string literal +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("SyntaxError")}} </p> + +<h2 id="哪里出错了?">哪里出错了?</h2> + +<p>某处 js 解析字符串出错。字符串必须使用单引号或双引号来正确的关闭。在 Javascript 中使用单引号的字符和双引号的字符串是没有区别的。字符串用<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#Escape_notation">转义字符</a>不是单引号就是双引。为解决这个错误,检查一下:</p> + +<ul> + <li>你字符串的引号是否成对。</li> + <li>你是否正确使用了转义序列</li> + <li>你的字符串是否在多行中解析正常。</li> +</ul> + +<h2 id="示例">示例</h2> + +<h3 id="多行字符串">多行字符串</h3> + +<p>在javascript中你不能够直接使用多行字符串赋值给一个变量。如下:</p> + +<pre class="brush: js example-bad">var longString = "This is a very long string which needs + to wrap across multiple lines because + otherwise my code is unreadable."; +// SyntaxError: unterminated string literal</pre> + +<p>可以使用<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Addition">"+"运算符</a>,反斜杠,或<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals">模板字符串</a>来代替多行。“+”运算符的使用如下:</p> + +<pre class="brush: js example-good">var longString = "This is a very long string which needs " + + "to wrap across multiple lines because " + + "otherwise my code is unreadable."; +</pre> + +<p>或者你可以使用“\”在每一行的末尾,以表示该字符串在下一行继续。要确保“\“之后没有没有空格和任何其他的字符,及缩进,否则该“\”将不会起作用。使用方法如下:</p> + +<pre class="brush: js example-good">var longString = "This is a very long string which needs \ +to wrap across multiple lines because \ +otherwise my code is unreadable."; +</pre> + +<p>另一种方式是使用 ES 2015 的环境所支持<a href="/zh-CN/docs/Web/JavaScript/Reference/Template_literals">模板字符串</a>(反引号` `)。</p> + +<pre class="brush: js example-good">var longString = `This is a very long string which needs + to wrap across multiple lines because + otherwise my code is unreadable.`;</pre> + +<h2 id="相关">相关</h2> + +<ul> + <li>{{jsxref("String")}} </li> + <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals">模板字符串</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/var_hides_argument/index.html b/files/zh-cn/web/javascript/reference/errors/var_hides_argument/index.html new file mode 100644 index 0000000000..01aa0260fe --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/var_hides_argument/index.html @@ -0,0 +1,51 @@ +--- +title: 'TypeError: variable "x" redeclares argument' +slug: Web/JavaScript/Reference/Errors/Var_hides_argument +translation_of: Web/JavaScript/Reference/Errors/Var_hides_argument +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="消息">消息</h2> + +<pre class="syntaxbox">TypeError: variable "x" redeclares argument (Firefox) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("TypeError")}} 警告仅仅在 <a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">严格模式下</a> 出现。</p> + +<h2 id="哪里有问题?">哪里有问题?</h2> + +<p>函数参数中出现了名称相同的变量,之后在函数体中使用 <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/var">var</a></code> 赋值语句重新声明。 这可能是一个命名冲突,所以 JavaScript 警告了它。</p> + +<p>这个错误只在 <a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">严格模式的代码</a> 中作为警告出现。在非严格模式的代码中,重新声明会被忽略。</p> + +<h2 id="示例">示例</h2> + +<h3 id="无效情况">无效情况</h3> + +<p>这个例子中,变量 <code>arg</code> 重新声明了参数。</p> + +<pre class="brush: js example-bad">"use strict"; + +function f(arg) { + var arg = "foo"; +} +</pre> + +<h3 id="无效情况_2">无效情况</h3> + +<p>为了修复警告,<code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/var">var</a></code> 语句应该被移除,因为变量已经存在。或者,你可以重命名函数参数或者变量名称。</p> + +<pre class="brush: js example-good">"use strict"; + +function f(arg) { + arg = "foo"; +} +</pre> + +<h2 id="另见">另见</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">严格模式</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/errors/不能添加属性/index.html b/files/zh-cn/web/javascript/reference/errors/不能添加属性/index.html new file mode 100644 index 0000000000..cceeb330c4 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/不能添加属性/index.html @@ -0,0 +1,52 @@ +--- +title: 'TypeError: can''t assign to property "x" on "y": not an object' +slug: Web/JavaScript/Reference/Errors/不能添加属性 +translation_of: Web/JavaScript/Reference/Errors/Cant_assign_to_property +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="信息">信息</h2> + +<pre class="syntaxbox">TypeError: <code class="highlighted" id="line-87">can't assign to property "x" on {y}: not an object</code> (Firefox) +TypeError: Cannot create property 'x' on {y} (Chrome) +</pre> + +<h2 id="错误类型">错误类型</h2> + +<p>{{jsxref("TypeError")}}.</p> + +<h2 id="原因">原因</h2> + +<p>在 {{jsxref("Strict_mode")}}下, 当试图给一个{{Glossary("symbol")}},{{Glossary("string")}},{{Glossary("number")}}或者一个{{Glossary("boolean")}}类型的数据创建一个属性时就会报 {{jsxref("TypeError")}}, 任何 {{Glossary("Primitive")}} 值都不允许有{{Glossary("property/JavaScript", "property")}}.</p> + +<p>这个问题可能是由一个错误的值被放在了一个错误的地方导致的, 或者预期{{jsxref("String")}}或{{jsxref("Number")}}的对象变体</p> + +<p> </p> + +<h2 id="示例">示例</h2> + +<h3 id="错误的情况">错误的情况</h3> + +<pre class="brush: js example-bad">'use strict'; + +var foo = "my string"; +// 下面这行代码在非严格模式下不会执行. +foo.bar = {}; // <span class="message-body-wrapper"><span class="message-flex-body"><span class="devtools-monospace message-body">TypeError: can't assign to property "bar" on "my string": not an object</span></span></span> +</pre> + +<h3 id="如何正确使用">如何正确使用</h3> + +<p>有两种方式, 第一种修复这部分代码阻止{{Glossary("primitive")}}被用于这种情况, 或者可以通过使用对象构造器创建来修复.</p> + +<pre class="brush: js example-good">'use strict'; + +var foo = new String("my string"); +foo.bar = {}; +</pre> + +<h2 id="页面相关">页面相关</h2> + +<ul> + <li>{{jsxref("Strict_mode")}}</li> + <li>{{Glossary("primitive")}}</li> +</ul> |