TypeError: "x" is not a function
It attempted to call a value from 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 the JavaScript Array object does.
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
or TypedArray
objects: Map
and Set
objects: In this case, which happens way too often, there is a typo in the method name:
var x = document.getElementByID('foo'); // TypeError: document.getElementByID is not a function
The correct function name is 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
Use an array instead:
var numbers = [1, 4, 9]; numbers.map(function(num) { return num * 2; }); // Array [2, 8, 18]
Sometimes when making a class, you may have a property and a function with the same name. Upon calling the function, the compiler thinks that the function ceases to exist.
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
Use a different property name instead:
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' }
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Not_a_function