1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
---
title: 'TypeError: "x" nie jest funkcją'
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="Wiadomość">Wiadomość</h2>
<pre class="syntaxbox">TypeError: "x" is not a function
</pre>
<h2 id="Typ_błędu">Typ błędu</h2>
<p>{{jsxref("TypeError")}}.</p>
<h2 id="Co_poszło_źle">Co poszło źle?</h2>
<p>Próbowano wywołać wartość jak funkcję, ale wartość nie jest funkcją. Kod oczekuje od Ciebie przekazania funkcji, co nie miało miejsca.</p>
<p>Może zrobiłeś literówkę w nazwie funkcji? Może objekt na którym wywołujesz tę metodę nie posiada tej funkcji? Na przykład, objekt JavaScript nie posiada funkcji <em>map, </em>natomiast objekt JavaScript Array posiada.</p>
<p>Istnieje mnóstwo wbudowanych funkcji wymagających (callback) funkcji. Będziesz musiał wprowadzić funkcję, by metody te działały poprawnie:</p>
<ul>
<li>Gdy pracujesz z {{jsxref("Array")}} lub {{jsxref("TypedArray")}} objektami:
<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>Gdy pracujesz z {{jsxref("Map")}} i {{jsxref("Set")}} objektami:
<ul>
<li>{{jsxref("Map.prototype.forEach()")}} and {{jsxref("Set.prototype.forEach()")}}</li>
</ul>
</li>
</ul>
<h2 id="Przykłady">Przykłady</h2>
<h3 id="Literówka_w_nazwie_funkcji">Literówka w nazwie funkcji</h3>
<p>W tym wypadku, co zdarza się zbyt często, jest literówka w nazwie metody:</p>
<pre class="brush: js example-bad">var x = document.getElementByID('foo');
// TypeError: document.getElementByID is not a function
</pre>
<p>Poprawna naywa metody to <code>getElementByI<strong>d</strong></code>:</p>
<pre class="brush: js example-good">var x = document.getElementById('foo');
</pre>
<h3 id="Funkcje_wywołane_na_nieodpowiednim_objekcie">Funkcje wywołane na nieodpowiednim objekcie</h3>
<p>Dla pewnych metod, musisz podać (callback) funkcję, która będzie działała tylko dla specyficznych objektów. W tym przykładzie, {{jsxref("Array.prototype.map()")}} jest użyta, podczas gdy działa tylko z objektami {{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>Użyj w zamian <em>array:</em></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="Zobacz_również">Zobacz również</h2>
<ul>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Functions">Functions</a></li>
</ul>
|