javascript – Why does Illegal invocation occur in JS?


I'm using GopherJS, and in it there are two ways to execute a function, via Call() or via Invoke() . Initially I was using code similar to this:

    password.New(js.M{"id": id, "password": pass, "name": email}),

But, as a result:

Uncaught (in promise) TypeError: Failed to execute 'store' on 'CredentialsContainer': Illegal invocation at :1:36

So, I decided to test the Call , using js.Global.Get("navigator").Get("credentials").Call("store", ...) , it worked, but I was curious why Invoke didn't work.

In Javascript, it looks like it has similar behavior if you do:

x =; 
x(new PasswordCredential({id: "a", password:"b"}))

Or, if you do:
    new PasswordCredential({id: "a", password:"b"})

In both cases it results in the error of Illegal invocation .

Why does this error occur? Why only happens with some functions/objects? Is there a way to identify if I can use Invoke (or Javascript Call ?) before causing the error?


In Go: store is the method, so we need to use Call . Invoke only works on functions. There is a difference, see below.

In JS: a method is not like a normal function as it has this . You can use a method like a function like this:

// Não funciona.
x = window.document.querySelector;
elem = x("a");
// Funciona, observe o bind().
x = window.document.querySelector.bind(window.document);
elem = x("a");
// Também funciona.
x = window.document.querySelector;
elem =, "a");

Documentation Links:

Scroll to Top