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, 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’.



HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    var GFG_Set = new Set;
    ['Geek', 'GFG'].forEach(GFG_Set.add);
</script>

chevron_right


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’.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    var GFG_Fun = function () {
      console.log(this);
    };
    ['Geek', 'GFG'].forEach(GFG_Fun.bind);
</script>

chevron_right


Output(in console):

TypeError: 'this' is not a Set object

full-stack-img

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.