Invalid caller error in IE

So, I get the error "Invalid Calling Object" in IE11 when I try to do the following:

window.toString.call({}); 

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

This form works though:

 ({}).toString(); 

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

+6
source share
1 answer

You seem to neglect the fact

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

The toString window is implementation-specific, and there is nothing in the specification that says that methods on Host DOM objects should work with call / on other / etc objects

If you want to capture this toString but cannot accept the prototype, try

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

You can also consider skipping a 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]" 
+13
source

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


All Articles