You’re reading the English version of this content since no translation exists yet for this locale. Help us translate this article!
הודעה
TypeError: "x" is not a function
סוג השגיאה
מה השתבש?
It attempted to call a value like a function, but the value is not actually a function. Some code expects you to provide a function, but that didn't happen.
Maybe there is a typo in the function name? Maybe the object you are calling the method on does not have this function? For example, JavaScript objects have no map function, but JavaScript Array object do.
There are many built-in functions in need of a (callback) function. You will have to provide a function in order to have these methods working properly:
- בעת עבודה עם הפריטים
ArrayאוTypedArray: - בעת עבודה עם הפריטים
Mapו־Set:
דוגמאות
טעות בשם הפונקציה
במקרה כזה, שקורה לעתים קרובות, יש טעות בשם המתודה:
var x = document.getElementByID("foo");
// TypeError: document.getElementByID is not a function
השם הנכון של הפונקציה הוא getElementById:
var x = document.getElementById("foo");
הפונקציה קראה לפריט הלא נכון
For certain methods, you have to provide a (callback) function and it will work on specific objects only. In this example, Array.prototype.map() is used, which will work with Array objects only.
var obj = { a: 13, b: 37, c: 42 };
obj.map(function(num) {
return num * 2;
});
// TypeError: obj.map is not a function
ניתן להשתמש במערך במקום:
var numbers = [1, 4, 9];
numbers.map(function(num) {
return num * 2;
});
// Array [ 2, 8, 18 ]