Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptJavaScript TypeError – X.prototype.y called on incompatible type

JavaScript TypeError – X.prototype.y called on incompatible type

This JavaScript exception called on incompatible target (or object)” occurs if a function (on a given object), is called with a ‘this’ corresponding to a different type other than the type expected by the function.

Message:

TypeError: 'this' is not a Set object (EdgE)
TypeError: Function.prototype.toString called on 
           incompatible object (Firefox)
TypeError: Function.prototype.bind called on incompatible
           target (Firefox)
TypeError: Method Set.prototype.add called on incompatible
           receiver undefined (Chrome)
TypeError: Bind must be called on a function (Chrome)

Error Type:

TypeError

Cause of Error: Somewhere in function calls, a function (on a given object), is called with a ‘this’ not corresponding to the type expected by the function.

Example 1:  In this example, the GFG_Set.add is a function, but GFG_Set is not captured as ‘this’.

Javascript




let GFG_Set = new Set;
['Geek', 'GFG'].forEach(GFG_Set.add);


Output(in console):

TypeError: 'this' is not a Set object

Example 2: In this example, GFG_Fun.bind is a function, but GFG_Fun is not captured as ‘this’.

Javascript




let GFG_Fun = function () {
    console.log(this);
};
['Geek', 'GFG'].forEach(GFG_Fun.bind);


Output(in console):

TypeError: 'this' is not a Set object
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments