You have a local variable called colour
in the for...in
loop, and that is not a function. Change that variable to something other than the constructor function, like c
and it’ll work fine. Your code, corrected:
var bodies = [];
var orbits = [];
var colours = [
new colour(45, 45, 45),
new colour(255, 0, 0),
new colour(0, 157, 255),
new colour(77, 250, 81),
new colour(255, 247, 0)
];
function colour(red, green, blue)
{
this.red = red;
this.green = green;
this.blue = blue;
};
window.onload = function() {
var c = document.getElementById("theCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.arc(95,50,40,0,2*Math.PI);
ctx.stroke();
for (var c in colours){
console.log(c.red);
console.log(c.green);
console.log(c.blue);
};
var myColour = new colour(255,255,255);
console.log(myColour.red);
console.log(myColour.green);
console.log(myColour.blue);
};
Demo
Время на прочтение
5 мин
Количество просмотров 397K
JavaScript может быть кошмаром при отладке: некоторые ошибки, которые он выдает, могут быть очень трудны для понимания с первого взгляда, и выдаваемые номера строк также не всегда полезны. Разве не было бы полезно иметь список, глядя на который, можно понять смысл ошибок и как исправить их? Вот он!
Ниже представлен список странных ошибок в JavaScript. Разные браузеры могут выдавать разные сообщения об одинаковых ошибках, поэтому приведено несколько примеров там, где возможно.
Как читать ошибки?
Перед самим списком, давайте быстро взглянем на структуру сообщения об ошибке. Понимание структуры помогает понимать ошибки, и вы получите меньше проблем, если наткнетесь на ошибки, не представленные в этом списке.
Типичная ошибка из Chrome выглядит так:
Uncaught TypeError: undefined is not a function
Структура ошибки следующая:
- Uncaught TypeError: эта часть сообщения обычно не особо полезна.
Uncaught
значит, что ошибка не была перехвачена вcatch
, аTypeError
— это название ошибки. - undefined is not a function: это та самая часть про ошибку. В случае с сообщениями об ошибках, читать их нужно прямо буквально. Например, в этом случае, она значит то, что код попытался использовать значение
undefined
как функцию.
Другие webkit-браузеры, такие как Safari, выдают ошибки примерно в таком же формате, как и Chrome. Ошибки из Firefox похожи, но не всегда включают в себя первую часть, и последние версии Internet Explorer также выдают более простые ошибки, но в этом случае проще — не всегда значит лучше.
Теперь к самим ошибкам.
Uncaught TypeError: undefined is not a function
Связанные ошибки: number is not a function, object is not a function, string is not a function, Unhandled Error: ‘foo’ is not a function, Function Expected
Возникает при попытке вызова значения как функции, когда значение функцией не является. Например:
var foo = undefined;
foo();
Эта ошибка обычно возникает, если вы пытаетесь вызвать функцию для объекта, но опечатались в названии.
var x = document.getElementByID('foo');
Несуществующие свойства объекта по-умолчанию имеют значение undefined
, что приводит к этой ошибке.
Другие вариации, такие как “number is not a function” возникают при попытке вызвать число, как будто оно является функцией.
Как исправить ошибку: убедитесь в корректности имени функции. Для этой ошибки, номер строки обычно указывает в правильное место.
Uncaught ReferenceError: Invalid left-hand side in assignment
Связанные ошибки: Uncaught exception: ReferenceError: Cannot assign to ‘functionCall()’, Uncaught exception: ReferenceError: Cannot assign to ‘this’
Вызвано попыткой присвоить значение тому, чему невозможно присвоить значение.
Наиболее частый пример этой ошибки — это условие в if:
if(doSomething() = 'somevalue')
В этом примере программист случайно использовал один знак равенства вместо двух. Выражение “left-hand side in assignment” относится к левой части знака равенства, а, как можно видеть в данном примере, левая часть содержит что-то, чему нельзя присвоить значение, что и приводит к ошибке.
Как исправить ошибку: убедитесь, что вы не пытаетесь присвоить значение результату функции или ключевому слову this
.
Uncaught TypeError: Converting circular structure to JSON
Связанные ошибки: Uncaught exception: TypeError: JSON.stringify: Not an acyclic Object, TypeError: cyclic object value, Circular reference in value argument not supported
Всегда вызвано циклической ссылкой в объекте, которая потом передается в JSON.stringify
.
var a = { };
var b = { a: a };
a.b = b;
JSON.stringify(a);
Так как a
и b
в примере выше имеют ссылки друг на друга, результирующий объект не может быть приведен к JSON.
Как исправить ошибку: удалите циклические ссылки, как в примере выше, из всех объектов, которые вы хотите сконвертировать в JSON.
Unexpected token ;
Связанные ошибки: Expected ), missing ) after argument list
Интерпретатор JavaScript что-то ожидал, но не обнаружил там этого. Обычно вызвано пропущенными фигурными, круглыми или квадратными скобками.
Токен в данной ошибке может быть разным — может быть написано “Unexpected token ]”, “Expected {” или что-то еще.
Как исправить ошибку: иногда номер строки не указывает на правильное местоположение, что затрудняет исправление ошибки.
Ошибка с [ ] { } ( ) обычно вызвано несовпадающей парой. Проверьте, все ли ваши скобки имеют закрывающую пару. В этом случае, номер строки обычно указывает на что-то другое, а не на проблемный символ.
Unexpected / связано с регулярными выражениями. Номер строки для данного случая обычно правильный.
Unexpected; обычно вызвано символом; внутри литерала объекта или массива, или списка аргументов вызова функции. Номер строки обычно также будет верным для данного случая.
Uncaught SyntaxError: Unexpected token ILLEGAL
Связанные ошибки: Unterminated String Literal, Invalid Line Terminator
В строковом литерале пропущена закрывающая кавычка.
Как исправить ошибку: убедитесь, что все строки имеют правильные закрывающие кавычки.
Uncaught TypeError: Cannot read property ‘foo’ of null, Uncaught TypeError: Cannot read property ‘foo’ of undefined
Связанные ошибки: TypeError: someVal is null, Unable to get property ‘foo’ of undefined or null reference
Попытка прочитать null
или undefined
так, как будто это объект. Например:
var someVal = null;
console.log(someVal.foo);
Как исправить ошибку: обычно вызвано опечатками. Проверьте, все ли переменные, использованные рядом со строкой, указывающей на ошибку, правильно названы.
Uncaught TypeError: Cannot set property ‘foo’ of null, Uncaught TypeError: Cannot set property ‘foo’ of undefined
Связанные ошибки: TypeError: someVal is undefined, Unable to set property ‘foo’ of undefined or null reference
Попытка записать null
или undefined
так, как будто это объект. Например:
var someVal = null;
someVal.foo = 1;
Как исправить ошибку: это тоже обычно вызвано ошибками. Проверьте имена переменных рядом со строкой, указывающей на ошибку.
Uncaught RangeError: Maximum call stack size exceeded
Связанные ошибки: Uncaught exception: RangeError: Maximum recursion depth exceeded, too much recursion, Stack overflow
Обычно вызвано неправильно программной логикой, что приводит к бесконечному вызову рекурсивной функции.
Как исправить ошибку: проверьте рекурсивные функции на ошибки, которые могут вынудить их делать рекурсивные вызовы вечно.
Uncaught URIError: URI malformed
Связанные ошибки: URIError: malformed URI sequence
Вызвано некорректным вызовом decodeURIComponent
.
Как исправить ошибку: убедитесь, что вызовы decodeURIComponent на строке ошибки получают корректные входные данные.
XMLHttpRequest cannot load some/url. No ‘Access-Control-Allow-Origin’ header is present on the requested resource
Связанные ошибки: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at some/url
Эта проблема всегда связана с использованием XMLHttpRequest.
Как исправить ошибку: убедитесь в корректности запрашиваемого URL и в том, что он удовлетворяет same-origin policy. Хороший способ найти проблемный код — посмотреть на URL в сообщении ошибки и найти его в своём коде.
InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable
Связанные ошибки: InvalidStateError, DOMException code 11
Означает то, что код вызвал функцию, которую нельзя было вызывать в текущем состоянии. Обычно связано c XMLHttpRequest
при попытке вызвать на нём функции до его готовности.
var xhr = new XMLHttpRequest();
xhr.setRequestHeader('Some-Header', 'val');
В данном случае вы получите ошибку потому, что функция setRequestHeader
может быть вызвана только после вызова xhr.open
.
Как исправить ошибку: посмотрите на код в строке, указывающей на ошибку, и убедитесь, что он вызывается в правильный момент или добавляет нужные вызовы до этого (как с xhr.open
).
Заключение
JavaScript содержит в себе одни из самых бесполезных ошибок, которые я когда-либо видел, за исключением печально известной Expected T_PAAMAYIM_NEKUDOTAYIM
в PHP. Большая ознакомленность с ошибками привносит больше ясности. Современные браузеры тоже помогают, так как больше не выдают абсолютно бесполезные ошибки, как это было раньше.
Какие самые непонятные ошибки вы встречали? Делитесь своими наблюдениями в комментариях.
P.S. Этот перевод можно улучшить, отправив PR здесь.
Table of Contents
Hide
- What is TypeError: substring is not a function error?
- How to fix TypeError: substring is not a function error?
- Solution 1: Convert the value into a string
- Solution 2 – Performing the type check
- Conclusion
If we call the substring()
method on the value that is not of a string type, JavaScript will throw a TypeError: substring is not a function.
In this tutorial, we will look at what is TypeErrror: substring is not a function error and how to resolve them with examples.
Let us take a simple example to demonstrate this issue
const str = 1234567890
const output = str.substring(2, 3)
console.log(output)
Output
TypeError: str.substring is not a function
In the above example, we have declared a variable and assigned the integer value into it.
In the next statement, we call the String.substring()
method on the value of the type number, and hence we get a TypeError: substring is not a function.
We can also check the variable type using typeof()
to confirm the datatype.
const str = 1234567890
console.log("The type of variable is",typeof str)
Output
The type of variable is number
How to fix TypeError: substring is not a function error?
The substring()
method returns the part of the string
between the start and end indexes, or to the end of the string. The String.prototype.substring()
method can only be used on the string values and not on any other types.
There are two ways to fix this issue in JavaScript.
Solution 1: Convert the value into a string
We can easily resolve the issue by converting the value into a string before calling the substring()
method.
If we know the value can be converted to a valid string, then we can use the toString()
method in JavaScript that returns the string representing the object.
Let us take an example to resolve the issue using the toString()
method.
const str = 1234567890
// Convert to string and then call substring()
const output = str.toString().substring(2, 7)
console.log(output)
Output
34567
Solution 2 – Performing the type check
We can also perform a type check on the variable to check if it’s a string before calling the substring()
method.
Example – Type check using if/else
const str = "Hello World"
if (typeof str === 'string') {
// Convert to string and then call substring
const output = str.substring(6, str.length)
console.log(output)
}
else {
console.log("The object is not a valid string")
}
Output
World
Example – Type check using ternary operator
const str = "Hello World"
// Convert to string and then call substring
const result = typeof str === 'string' ? str.substring(5, str.length) : "";
console.log(result)
Output
World
Conclusion
The TypeError: substring is not a function occurs if we call a substring()
method on the value that is not of a type string. We can resolve the issue by converting the value into string before calling the substring()
method or by performing a type check; we can mitigate this error.
Related Tags
- substring(),
- toString(),
- TypeError,
- typeof
Sign Up for Our Newsletters
Get notified on the latest articles
By checking this box, you confirm that you have read and are agreeing to our terms of use regarding the storage of the data submitted through this form.
TypeError: «x» is not a function
The JavaScript exception «is not a function» occurs when there was an attempt to call a value from a function, but the value is not actually a function.
Message
TypeError: Object doesn't support property or method {x} (Edge) TypeError: "x" is not a function
Error type
TypeError
What went wrong?
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:
- When working with
Array
orTypedArray
objects:-
Array.prototype.every()
,Array.prototype.some()
,Array.prototype.forEach()
,Array.prototype.map()
,Array.prototype.filter()
,Array.prototype.reduce()
,Array.prototype.reduceRight()
,Array.prototype.find()
-
- When working with
Map
andSet
objects:-
Map.prototype.forEach()
andSet.prototype.forEach()
-
Examples
A typo in the function name
In this case, which happens way too often, there is a typo in the method name:
let x = document.getElementByID('foo');
The correct function name is getElementById
:
let x = document.getElementById('foo');
Function called on the wrong object
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.
let obj = {a: 13, b: 37, c: 42}; obj.map(function(num) { return num * 2; });
Use an array instead:
let numbers = [1, 4, 9]; numbers.map(function(num) { return num * 2; });
Function shares a name with a pre-existing property
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");
Use a different property name instead:
var Dog = function () { this.age = 11; this.color = "black"; this.dogName = "Ralph"; return this; } Dog.prototype.name = function(name) { this.dogName = name; return this; } var myNewDog = new Dog(); myNewDog.name("Cassidy");
Using brackets for multiplication
In math, you can write 2 × (3 + 5) as 2*(3 + 5) or just 2(3 + 5).
Using the latter will throw an error:
const sixteen = 2(3 + 5); alert('2 x (3 + 5) is ' + String(sixteen));
You can correct the code by adding a *
operator:
const sixteen = 2 * (3 + 5); alert('2 x (3 + 5) is ' + String(sixteen));
Import the exported module correctly
Ensure you are importing the module correctly.
An example helpers library (helpers.js
)
let helpers = function () { }; helpers.groupBy = function (objectArray, property) { return objectArray.reduce(function (acc, obj) { var key = obj[property]; if (!acc[key]) { acc[key] = []; } acc[key].push(obj); return acc; }, {}); } export default helpers;
The correct import usage (App.js
):
import helpers from './helpers'
See also
- Functions
Javascript split is not a function: When we call the split() method on a value that is not of type string, we get the “split is not a function” error.
To resolve the error, convert the value to a string using the toString() function before calling the split method, or only call the split method on strings.
Let us see the examples to know how this error occurs and try to fix them.
index.js:
// Create a date object and store it in a variable const gvn_str = new Date(); // Apply split() function on the date object and store it in a variable. // Here it throws an error since split() function cannot be applied on an object // It must be used only on strings const result = gvn_str.split(' ');
Output:
const result = gvn_str.split(' '); ^ TypeError: gvn_str.split is not a function at Object.<anonymous> (/tmp/ER7rLDlJor.js:4:24) at Module._compile (internal/modules/cjs/loader.js:778:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3) at Function.Module.runMain (internal/modules/cjs/loader.js:831:12) at startup (internal/bootstrap/node.js:283:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
Explanation:
Here we called the String.split() method on an object and returned an error. To fix the problem, only use the split() method on strings. Most values can be converted to strings using the function toString() method.
Using toString() Method for Conversion
// Create a Date object using the new keyword and // store it in a variable const gvn_string = new Date(); // Print the type of above variable using the typeof property console.log(typeof gvn_string); // Convert the above Date object to string using the // toString() method and split it based on '' using the // split() function const rslt = gvn_string.toString().split(' '); // Print the above result console.log(rslt);
Output:
object [ 'Sat', 'Jun', '11', '2022', '18:50:00', 'GMT+0000', '(GMT)' ]
Explanation:
We were able to call the split() function on the date object since the toString() method converted this date object to a string.
Using Ternary Operator to Check if it is a String and do Conversion if Necessary
Split is not a function: Alternatively, before executing the split()
method, you can check to see if the value is a string.
Example1: index.js
// Give some random value as static input and store it in // a variable const gvn_str = 50; // Check if the type of above variable value is a string using // the ternary operator. // If it is a string then split it based on ' ' using the // split() function else print 'Not a String' const rslt = typeof gvn_str === 'string' ? gvn_str.split(' '): 'Not a String'; // Print the above result console.log(rslt);
Output:
Not a String
Example2: index.js
// Give some random value as static input and store it in // a variable const gvn_str = "Hello this is Btechgeeks"; // Check if the type of above variable value is a string using // the ternary operator. // If it is a string then split it based on spaces(' ') using // the split() function else print 'Not a String' const rslt = typeof gvn_str === 'string' ? gvn_str.split(' '): 'Not a String'; // Print the above result console.log(rslt);
Output:
[ 'Hello', 'this', 'is', 'Btechgeeks' ]
Explanation:
- To determine whether the gvn_str variable contains a string, we utilized a ternary operator.
- If it satisfies the condition, it returns the value to the left of the colon(:) otherwise, it returns the value to the right.
- If it is a string then split it based on spaces(‘ ‘) using the split() function else print ‘Not a String’.
- If the value is an object, there’s a significant possibility you’re forgetting to access a specific property on which the split() method must be called.