It's not necessary, it just makes the code's intent more clear. When you use the toString
standalone function, usually that'll refer to window.toString
- window
is an object, and objects inherit from Object.prototype
, so window.toString === Object.prototype.toString
. But relying on this sort of thing implicitly can be confusing and can produce bugs.
There's also no guarantee that there's not some other function you've defined called toString
, eg:
(() => {
const toString = () => 'here is a string';
// many lines of code here
class Foo {}
const f = new Foo();
// Does not result in "[object Object]":
console.log(toString.call(f));
})();
Explicitly using Object.prototype.toString
instead makes the code more understandable and predictable at a glance.
(You're still free to use toString
alone instead if you want, it probably won't break anything, but it's probably not a great idea in general.)