I'm just curious,
This...
let s = { f: window.fetch };
s.f("https://www.google.com");
fails with
Failed to execute 'fetch' on 'Window': Illegal invocation
While this works...
let s = { f: window.fetch.bind(window) };
s.f("https://www.google.com");
How does the latter fix the issue? Is there any theory behind why it works that way?
For some internal purpose, the
fetchfunction must havethisbe the same aswindow. When you create your own object and just assign the fetch function as one of its properties, the fetch function has no access to thewindowobject asthis.The
fetchspecification describes things thewindowmight be used for. You might be able to make your original code work by settingno-window, but I have not tested that.