Is JavaScript “not a function” compared to the “expected function”?

In current Chrome, if I do this:

var i = 'foo'; i(); 

I get the error 'string is not a function' . I get similar errors if i is a number, undefined, etc.

However, from some real, more complex code, sometimes I see another error:

' expected function : function () {}'

I am trying to figure out how these two errors differ, or, to look at it differently, how to write minimal code that will throw an “expected function” error.

I tried messing around with callbacks and calling / applying, but none of them run this. Can someone explain how to reproduce this error?

+4
source share
1 answer

The error message specification is missing. Therefore, each supplier implements it independently. The only way to unify this is to check yourslef data and throw the errors you expect.

 var i = 'foo'; if (!$.isFunction(i)){ throw 'expected function: function(){}'; } 
0
source

Source: https://habr.com/ru/post/1481448/


All Articles