Как исправить ошибку string is not a function

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 мин

Количество просмотров 398K

JavaScript может быть кошмаром при отладке: некоторые ошибки, которые он выдает, могут быть очень трудны для понимания с первого взгляда, и выдаваемые номера строк также не всегда полезны. Разве не было бы полезно иметь список, глядя на который, можно понять смысл ошибок и как исправить их? Вот он!

Ниже представлен список странных ошибок в JavaScript. Разные браузеры могут выдавать разные сообщения об одинаковых ошибках, поэтому приведено несколько примеров там, где возможно.

Как читать ошибки?

Перед самим списком, давайте быстро взглянем на структуру сообщения об ошибке. Понимание структуры помогает понимать ошибки, и вы получите меньше проблем, если наткнетесь на ошибки, не представленные в этом списке.

Типичная ошибка из Chrome выглядит так:

Uncaught TypeError: undefined is not a function

Структура ошибки следующая:

  1. Uncaught TypeError: эта часть сообщения обычно не особо полезна. Uncaught значит, что ошибка не была перехвачена в catch, а TypeError — это название ошибки.
  2. 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 здесь.

This is running on applab, I am being given an error on line 55 of my code. It says TypeError: string is not a function. I can’t seem to figure out if it’s the function that it is in or somewhere in the code above that isn’t running properly, it would be much appreciated if someone could take a look and see what the problem is.

onEvent("startButton", "click", function() { 
  setScreen("safeScreen");
  endGame();
});//changes to the main games screen,after 2 minutes ends game
onEvent("retryButton", "click", function() { 
  setScreen("welcomeScreen");
});// changes screen to welcome screen from win screen
onEvent("againButto", "click", function() {
  setScreen("welcomeScreen");
});//changes screen to welcome screen from lose screen

function endGame(){
  setTimeout(function(){
    setScreen("loseScreen");
  },120000);
}
var input1=0;
setText("comboDisplay1",input1);//sets display1 to 0
var input2=0;
setText("comboDisplay2",input2);//sets display2 to 0
var input3=0;
setText("comboDisplay3",input3);//sets display3 to 0
onEvent("upButton1", "click", function(){
  input1 = input1 + 1;
  setText("comboDisplay1",input1);
});//when up button1 is pressed the number display changes
onEvent("upButton2", "click", function(){
  input2= input2 + 1;
  setText("comboDisplay2",input2);
});//when up button2 is pressed the number display changes
onEvent("upButton3", "click", function(){
  input3= input3 + 1;
  setText("comboDisplay3",input3);
});//when up button3 is pressed the display changes
onEvent("downButton1", "click", function(){
  input1 = input1 - 1;
  setText("comboDisplay1",input1);
});//when down button1 is pressed the number display changes
onEvent("downButton2", "click", function(){
  input2 = input2 - 1;
  setText("comboDisplay2",input2);
});//when down button2 is pressed the number display changes
onEvent("downButton3", "click", function(){
  input3 = input3 - 1;
  setText("comboDisplay3",input3);
});//when down button3 is pressed the number display changes

var playerInput;
var combination;
combination = [randomNumber(0,9),randomNumber(0,9),randomNumber(0,9)];//generates a random list of 3 numbers from 0 to 9
console.log(combination);

playerInput += getText("comboDisplay1","comboDisplay2","comboDisplay3");//assings the display numbers to the variable playerInput
function yellowLight() {
  if (getNumber("comboDisplay1") != combination(0)) {
    if (getNumber("comboDisplay1") == (combination(1)||combination(2))) {
      showElement("yellowLight1");
    }
  }
  if (getNumber("comboDisplay2") != combination(1)) {
    if (getNumber("comboDisplay2") == (combination(0) || combination(2))) {
      showElement("yellowLIght2");
    }
  }
  if (getNumber("comboDisplay3") != combination(3)) {
    if (getNumber("comboDisplay3") == (combination(0) || combination(1))) {
      showElement("yellowLight3");
    }
  }
}
function greenLight(){
  if(getNumber("comboDisplay1") == combination(0));
    showElement("greenLight1");
  if(getNumber("comboDisplay2") == combination(1));
    showElement("greenLight2");
  if(getNumber("comboDisplay3") == combination(2));
    showElement("greenLight3");
}
//checks and shows a green light if the number is correct and in the right place
onEvent("submitButto", "click", function(){
  if( playerInput== combination){
    greenLight();
  }
    yellowLight();
});

asked Mar 23, 2017 at 13:47

Damian Meza's user avatar

3

The type of combination is array. You have to access its element by using [], not (), e.g. combinations[0].

answered Mar 23, 2017 at 13:52

Szymon P.'s user avatar

Szymon P.Szymon P.

9986 silver badges11 bronze badges

0

Table of Contents

Hide

  1. What is TypeError: substring is not a function error?
  2. How to fix TypeError: substring is not a function error?
    1. Solution 1: Convert the value into a string
    2. Solution 2 – Performing the type check
  3. 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.

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 or TypedArray 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 and Set objects:
    • Map.prototype.forEach() and Set.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');
// TypeError: document.getElementByID is not a function

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;
});

// TypeError: obj.map is not a function

Use an array instead:

let numbers = [1, 4, 9];

numbers.map(function(num) {
  return num * 2;
});

// Array [2, 8, 18]

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"); //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' }

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));
//Uncaught TypeError: 2 is not a function

You can correct the code by adding a * operator:

const sixteen = 2 * (3 + 5);
alert('2 x (3 + 5) is ' + String(sixteen));
//2 x (3 + 5) is 16

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

Понравилась статья? Поделить с друзьями:
  • Как исправить ошибку stop c000021a
  • Как исправить ошибку stop 0x00000024
  • Как исправить ошибку setup error
  • Как исправить ошибку set user settings to driver failed
  • Как исправить ошибку stop 0 00000024