diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:43:23 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:43:23 -0500 |
commit | 218934fa2ed1c702a6d3923d2aa2cc6b43c48684 (patch) | |
tree | a9ef8ac1e1b8fe4207b6d64d3841bfb8990b6fd0 /files/zh-tw/web/javascript/reference/errors | |
parent | 074785cea106179cb3305637055ab0a009ca74f2 (diff) | |
download | translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.gz translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.bz2 translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.zip |
initial commit
Diffstat (limited to 'files/zh-tw/web/javascript/reference/errors')
10 files changed, 533 insertions, 0 deletions
diff --git a/files/zh-tw/web/javascript/reference/errors/bad_return_or_yield/index.html b/files/zh-tw/web/javascript/reference/errors/bad_return_or_yield/index.html new file mode 100644 index 0000000000..7a28fb4be3 --- /dev/null +++ b/files/zh-tw/web/javascript/reference/errors/bad_return_or_yield/index.html @@ -0,0 +1,51 @@ +--- +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="/zh-TW/docs/Web/JavaScript/Reference/Statements/return">return</a></code> 或 <code><a href="/zh-TW/docs/Web/JavaScript/Reference/Operators/yield">yield</a></code> 宣告在<a href="/zh-TW/docs/Web/JavaScript/Guide/Functions">函式</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>乍看之下大括號寫對了,但其實在第一個 <code>if</code> 的後面,少了一個 <code>{</code>。正確的寫法應該是:</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="/zh-TW/docs/Web/JavaScript/Reference/Statements/return">return</a></code></li> + <li><code><a href="/zh-TW/docs/Web/JavaScript/Reference/Operators/yield">yield</a></code></li> +</ul> diff --git a/files/zh-tw/web/javascript/reference/errors/index.html b/files/zh-tw/web/javascript/reference/errors/index.html new file mode 100644 index 0000000000..8f553faa75 --- /dev/null +++ b/files/zh-tw/web/javascript/reference/errors/index.html @@ -0,0 +1,22 @@ +--- +title: JavaScript 錯誤參考資料 +slug: Web/JavaScript/Reference/Errors +translation_of: Web/JavaScript/Reference/Errors +--- +<p>{{jsSidebar("Errors")}}</p> + +<p>在這裡,你可以看到一些由 JavaScript 拋出的錯誤一覽。這些錯誤訊息對你的除錯很有幫助,但拋出的錯誤也不是每次都能講清楚。本頁回提供這些錯誤的詳細資訊。所有的錯誤都是由 {{jsxref("Error")}} 物件所建立的物件,有著 <code>name</code> 與 <code>message</code>。</p> + +<p>錯誤會出現在網路主控台、可能還連接到相應頁面,以幫助你儘速理解程式碼裡面的問題。</p> + +<h2 id="錯誤一覽表">錯誤一覽表</h2> + +<p>In this list, each page is listed by name (the type of error) and message (a more detailed human-readable error message). Together, these two properties provide a starting point toward understanding and resolving the error. For more information, follow the links below!</p> + +<p>{{ListSubPages("/zh-TW/docs/Web/JavaScript/Reference/Errors")}}</p> + +<h2 id="參見">參見</h2> + +<ul> + <li><a href="/zh-TW/docs/Learn/JavaScript/First_steps/What_went_wrong">哪裡出錯了?JavaScript 除錯</a>:針對初學者的 JavaScript 除錯入門教程。</li> +</ul> diff --git a/files/zh-tw/web/javascript/reference/errors/invalid_array_length/index.html b/files/zh-tw/web/javascript/reference/errors/invalid_array_length/index.html new file mode 100644 index 0000000000..ee2c5f08e4 --- /dev/null +++ b/files/zh-tw/web/javascript/reference/errors/invalid_array_length/index.html @@ -0,0 +1,74 @@ +--- +title: 'RangeError: invalid array length' +slug: Web/JavaScript/Reference/Errors/Invalid_array_length +translation_of: Web/JavaScript/Reference/Errors/Invalid_array_length +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="訊息">訊息</h2> + +<pre class="syntaxbox">RangeError: Array length must be a finite positive integer (Edge) +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>的 {{jsxref("Array")}} 或 {{jsxref("ArrayBuffer")}} </li> + <li>將 {{jsxref("Array.length")}} 屬性設為負值或大於等於 2<sup>32</sup></li> +</ul> + +<p>為什麼 <code>Array</code> 和 <code>ArrayBuffer</code> 的長度有限? <code>Array</code> 和 <code>ArrayBuffer</code> 的屬性以一個32位元的非負整數表使,因此僅能儲存 0 到 2<sup>32</sup>-1 的數值。</p> + +<p>If you are creating an <code>Array</code>, using the constructor, you probably want to use the literal notation instead, as the first argument is interpreted as the length of the <code>Array</code>.</p> + +<p>Otherwise, you might want to clamp the length before setting the length property, or using it as argument of the constructor.</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; // set -1 to the length property + +let b = new Array(Math.pow(2, 32) - 1); +b.length = b.length + 1; // set 2^32 to the length property +</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-tw/web/javascript/reference/errors/missing_curly_after_property_list/index.html b/files/zh-tw/web/javascript/reference/errors/missing_curly_after_property_list/index.html new file mode 100644 index 0000000000..7e3728fc49 --- /dev/null +++ b/files/zh-tw/web/javascript/reference/errors/missing_curly_after_property_list/index.html @@ -0,0 +1,47 @@ +--- +title: 'SyntaxError: missing } after property list' +slug: Web/JavaScript/Reference/Errors/Missing_curly_after_property_list +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>在物件初始化時,語法錯誤。 實際上可能遺漏一個大括號或是逗號。 例如, 同時檢查大括弧以及逗號是否以正確的順序關閉。 縮排或是有規則的排序代碼是有幫助您找出複雜的代碼錯誤。</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>Correct would be:</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-tw/web/javascript/reference/errors/no_properties/index.html b/files/zh-tw/web/javascript/reference/errors/no_properties/index.html new file mode 100644 index 0000000000..b355d15ea3 --- /dev/null +++ b/files/zh-tw/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; +// TypeError: null has no properties + +undefined.bar; +// TypeError: undefined has no properties +</pre> + +<h2 id="參見">參見</h2> + +<ul> + <li>{{jsxref("null")}}</li> + <li>{{jsxref("undefined")}}</li> +</ul> diff --git a/files/zh-tw/web/javascript/reference/errors/not_a_function/index.html b/files/zh-tw/web/javascript/reference/errors/not_a_function/index.html new file mode 100644 index 0000000000..24ce79a6e4 --- /dev/null +++ b/files/zh-tw/web/javascript/reference/errors/not_a_function/index.html @@ -0,0 +1,80 @@ +--- +title: 'TypeError: "x" is not a function' +slug: Web/JavaScript/Reference/Errors/Not_a_function +translation_of: Web/JavaScript/Reference/Errors/Not_a_function +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="訊息">訊息</h2> + +<pre class="syntaxbox">TypeError: "x" is not a function +</pre> + +<h2 id="錯誤類型">錯誤類型</h2> + +<p>{{jsxref("TypeError")}}.</p> + +<h2 id="哪裡錯了?">哪裡錯了?</h2> + +<p>你想以函式呼叫一個數值,但該數值其實不是函式。程式碼期望你給出函式,但這份期望落空了。</p> + +<p>也許打錯了函式的名字?也許呼叫的物件並沒有這個函式?例如說 JavaScript 物件並沒有 <code>map</code> 函式,但 JavaScript Array(陣列)物件則有。</p> + +<p>許多內建函式都需要回呼(callback)的函式。為了讓下面的方法順利運作,你需要為它們提供函式:</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()")}} 與 {{jsxref("Set.prototype.forEach()")}}</li> + </ul> + </li> +</ul> + +<h2 id="實例">實例</h2> + +<h3 id="函式的名字打錯了">函式的名字打錯了</h3> + +<p>這種事太常發生了。下例就有個方法打錯:</p> + +<pre class="brush: js example-bad">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">var x = document.getElementById("foo"); +</pre> + +<h3 id="函式呼叫到錯誤的物件">函式呼叫到錯誤的物件</h3> + +<p>某些方法需要你提供回呼的函式,該函式只能作用於特定物件。以本例而言,我們使用的 {{jsxref("Array.prototype.map()")}} 就只能作用於 {{jsxref("Array")}} 物件。</p> + +<pre class="brush: js example-bad">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">var numbers = [1, 4, 9]; + +numbers.map(function(num) { + return num * 2; +}); + +// Array [ 2, 8, 18 ] +</pre> + +<h2 id="參見">參見</h2> + +<ul> + <li><a href="/zh-TW/docs/Web/JavaScript/Reference/Functions">Functions</a></li> +</ul> diff --git a/files/zh-tw/web/javascript/reference/errors/not_defined/index.html b/files/zh-tw/web/javascript/reference/errors/not_defined/index.html new file mode 100644 index 0000000000..fa79033977 --- /dev/null +++ b/files/zh-tw/web/javascript/reference/errors/not_defined/index.html @@ -0,0 +1,67 @@ +--- +title: 'ReferenceError: "x" is not defined' +slug: Web/JavaScript/Reference/Errors/Not_defined +translation_of: Web/JavaScript/Reference/Errors/Not_defined +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="訊息">訊息</h2> + +<pre class="syntaxbox">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">foo.substring(1); // ReferenceError: foo is not defined +</pre> + +<p>"foo" 變數在任何地方都沒被定義到。它需要字串使 {{jsxref("String.prototype.substring()")}} 得以運作。</p> + +<pre class="brush: js example-good">var foo = "bar"; +foo.substring(1); // "ar"</pre> + +<h3 id="作用域錯誤">作用域錯誤</h3> + +<p>A variable need to be available in the current context of execution. Variables defined inside a <a href="/en-US/docs/Web/JavaScript/Reference/Functions">function</a> cannot be accessed from anywhere outside the function, because the variable is defined only in the scope of the function</p> + +<pre class="brush: js example-bad">function numbers () { + var num1 = 2, + num2 = 3; + return num1 + num2; +} + +console.log(num1); // ReferenceError num1 is not defined.</pre> + +<p>However, a function can access all variables and functions defined inside the scope in which it is defined. In other words, a function defined in the global scope can access all variables defined in the global scope.</p> + +<pre class="brush: js example-good">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="/zh-TW/docs/Web/JavaScript/Guide/Grammar_and_types#Declaring_variables">Declaring variables in the JavaScript Guide</a></li> + <li><a href="/zh-TW/docs/Web/JavaScript/Guide/Functions#Function_scope/en-US/docs/">Function scope in the JavaScript Guide</a></li> +</ul> diff --git a/files/zh-tw/web/javascript/reference/errors/redeclared_parameter/index.html b/files/zh-tw/web/javascript/reference/errors/redeclared_parameter/index.html new file mode 100644 index 0000000000..e9ba8cbbe0 --- /dev/null +++ b/files/zh-tw/web/javascript/reference/errors/redeclared_parameter/index.html @@ -0,0 +1,57 @@ +--- +title: 'SyntaxError: redeclaration of formal parameter "x"' +slug: Web/JavaScript/Reference/Errors/Redeclared_parameter +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) +</pre> + +<h2 id="錯誤類型">錯誤類型</h2> + +<p>{{jsxref("SyntaxError")}}</p> + +<h2 id="哪裡錯了?">哪裡錯了?</h2> + +<p>當相同的變數名作為函式的參數、接著又在函式體(function body)內用了 <code><a href="/zh-TW/docs/Web/JavaScript/Reference/Statements/let">let</a></code> 重複宣告並指派時出現。在 JavaScript 裡面,不允許在相同的函式、或是作用域區塊(block scope)內重複宣告相同的 <code>let</code> 變數。</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>If you want to change the value of "arg" in the function body, you can do so, but you do not need to declare the same variable again. In other words: you can omit the <code>let</code> keyword. If you want to create a new variable, you need to rename it as conflicts with the function parameter already.</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="/zh-TW/docs/Web/JavaScript/Reference/Statements/let">let</a></code></li> + <li><code><a href="/zh-TW/docs/Web/JavaScript/Reference/Statements/const">const</a></code></li> + <li><code><a href="/zh-TW/docs/Web/JavaScript/Reference/Statements/var">var</a></code></li> + <li>在 <a href="/zh-TW/docs/Web/JavaScript/Guide">JavaScript 教學</a>內<a href="/zh-TW/docs/Web/JavaScript/Guide/Grammar_and_Types#Declarations">宣告變數</a> </li> +</ul> diff --git a/files/zh-tw/web/javascript/reference/errors/too_much_recursion/index.html b/files/zh-tw/web/javascript/reference/errors/too_much_recursion/index.html new file mode 100644 index 0000000000..1708683ffa --- /dev/null +++ b/files/zh-tw/web/javascript/reference/errors/too_much_recursion/index.html @@ -0,0 +1,50 @@ +--- +title: 'InternalError: too much recursion' +slug: Web/JavaScript/Reference/Errors/Too_much_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>(recursive function)。在某些方面,遞迴和迴圈很像。它們都需要在指定條件(以避免無窮迴圈,或是本例的無窮遞迴)下,重複執行數次相同的程式碼。如果遞迴執行太多次、或成為無窮遞迴的話,JavaScript 就會出現這個錯誤。</p> + +<h2 id="實例">實例</h2> + +<p>以下的遞迴函式,會根據終止條件,而運行十次。</p> + +<pre class="brush: js">function loop(x) { + if (x >= 10) // "x >= 10" 是終止條件 + return; + // do stuff + loop(x + 1); // 遞迴呼叫 +} +loop(0);</pre> + +<p>如果把終止條件的次數設得太高,函式就不會運作了:</p> + +<pre class="brush: js example-bad">function loop(x) { + if (x >= 1000000000000) + return; + // do stuff + loop(x + 1); +} +loop(0); + +// InternalError: too much recursion</pre> + +<h2 id="參見">參見</h2> + +<ul> + <li>{{Glossary("Recursion")}}</li> + <li><a href="/zh-TW/docs/Web/JavaScript/Guide/Functions#Recursion">遞迴函式</a></li> +</ul> diff --git a/files/zh-tw/web/javascript/reference/errors/unexpected_type/index.html b/files/zh-tw/web/javascript/reference/errors/unexpected_type/index.html new file mode 100644 index 0000000000..d7399baf5d --- /dev/null +++ b/files/zh-tw/web/javascript/reference/errors/unexpected_type/index.html @@ -0,0 +1,49 @@ +--- +title: 'TypeError: "x" is (not) "y"' +slug: Web/JavaScript/Reference/Errors/Unexpected_type +translation_of: Web/JavaScript/Reference/Errors/Unexpected_type +--- +<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 和 null 的情況下在其上的子方法不起作用 +var foo = undefined; +foo.substring(1); // TypeError: foo is undefined + +var foo = null; +foo.substring(1); // TypeError: foo is null + + +// 某些方法可能要求特定類型 +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="/zh-TW/docs/Web/JavaScript/Reference/Operators/typeof">typeof</a> 運算符,例如。 operator, for example.</p> + +<pre class="brush: js">if (typeof foo !== 'undefined') { + // 現在我們知道foo被定義,我們可以繼續進行。 +}</pre> + +<h2 id="參見">參見</h2> + +<ul> + <li>{{jsxref("undefined")}}</li> + <li>{{jsxref("null")}}</li> +</ul> |