讨论 » 开发

Does userscript support static methods (object oriented)?

§
发布于:2020-07-27

Does userscript support static methods (object oriented)?

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 ?

woxxom管理员
§
发布于:2020-07-27

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.

§
发布于:2020-07-27

@wOxxOm 说道: 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.

The problem has been solved. Thanks.

发布留言

登录以发布留言。