diff options
| author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
|---|---|---|
| committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
| commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
| tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/ja/web/javascript/reference/errors/not_a_function | |
| parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
| download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip | |
initial commit
Diffstat (limited to 'files/ja/web/javascript/reference/errors/not_a_function')
| -rw-r--r-- | files/ja/web/javascript/reference/errors/not_a_function/index.html | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/files/ja/web/javascript/reference/errors/not_a_function/index.html b/files/ja/web/javascript/reference/errors/not_a_function/index.html new file mode 100644 index 0000000000..9b679e9bd2 --- /dev/null +++ b/files/ja/web/javascript/reference/errors/not_a_function/index.html @@ -0,0 +1,122 @@ +--- +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> + +<h2 id="Message" name="Message">エラーメッセージ</h2> + +<pre class="syntaxbox">TypeError: "x" is not a function +</pre> + +<h2 id="エラーの種類">エラーの種類</h2> + +<p>{{jsxref("TypeError")}}.</p> + +<h2 id="What_went_wrong" name="What_went_wrong">エラーの原因</h2> + +<p>関数でないものを、関数呼び出ししようとした際に発生するエラーです。また適切な関数が定義されていることを期待されているが、定義されていない場合も発生します。</p> + +<p>関数名のタイプミスをしていないか確認してみましょう。また、呼び出そうとしてるオブジェクトがそのメソッドを持っているかどうかも確認してみてください。配列オブジェクトが持っている <code>map</code> 関数を、それを持たない通常のオブジェクトに対して呼び出そうとしている場合が、後者の例になります。</p> + +<p>多くの組み込み関数はコールバック関数を必要とします。これらのメソッドを正しく呼び出すためには、関数を引数に指定する必要があります:</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> + +<h3 id="Function_shares_a_name_with_a_pre-existing_property" name="Function_shares_a_name_with_a_pre-existing_property">すでに存在するプロパティと名前を共有する関数</h3> + +<p>クラスを作るとき、時々プロパティと関数が同じ名前であることがあります。関数を呼び出すと、コンパイラーは関数が存在するのをやめたように考えます。</p> + +<pre><code>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</code></pre> + +<p>代わりに異なるプロパティ名を使ってください:</p> + +<pre><code>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' }</code></pre> + +<h2 id="See_also" name="See_also">関連情報</h2> + +<ul> + <li><a href="/ja/docs/Web/JavaScript/Reference/Functions">Functions</a></li> +</ul> |
