Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid Calling Object error in IE

So I'm getting the error 'Invalid Calling Object' in IE11 when trying to execute the following:

window.toString.call({});

When I expect to see => "[object Object]"

This form seems to works though:

({}).toString();

Both forms appear to work fine in chrome, am I missing something?

like image 868
Jaboc83 Avatar asked Oct 11 '13 15:10

Jaboc83


1 Answers

You seem to be neglecting the fact

window.toString === Object.prototype.toString; // false

The Window's toString is implementation-specific and there is nothing in the specification saying methods on DOM Host Objects have to work with call/on other objects/etc

If you want to capture this toString but can't assume prototype, try

var toString = ({}).toString;
toString.call({}); // "[object Object]"

You could also consider skipping call each time by wrapping it or using bind

var toString = function (x) { return ({}).toString.call(x); };
toString(10); // "[object Number]"
// or
var toString = ({}).toString.call.bind(({}).toString);
toString(true); // "[object Boolean]"
like image 162
Paul S. Avatar answered Nov 09 '22 02:11

Paul S.