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/not_a_function | |
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/not_a_function')
-rw-r--r-- | files/zh-tw/web/javascript/reference/errors/not_a_function/index.html | 80 |
1 files changed, 80 insertions, 0 deletions
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> |