The following code works in its own HTML, but it doesn't seem to work in userscript.

Foo.classMethod()

class Foo {
    static classMethod() {
        alert('hello')
        return 'hello';
    }
}

I googled for information about using object-oriented syntax in userscript. I didn't get anything. I just read the document and didn't find the answer.

Who knows what's going on ?

It "works" for you only because it's compiled into ES5 or otherwise reorganized by the bundler. The code is invalid by itself: class declarations are initialized in the order of their appearance in the source code as it's just a syntactic sugar for prototype assignments so you can use it only after it was declared. Move Foo.classMethod() to the end.

——wOxxOm