Invalid Calling Object error in IE

16,739

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]"
Share:
16,739
Jaboc83
Author by

Jaboc83

Married, software-developing, backpacking, photographing, cooking, father of three.

Updated on June 15, 2022

Comments

  • Jaboc83
    Jaboc83 about 2 years

    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?