Refer to class inside of static method without using its name

前端 未结 3 393
花落未央
花落未央 2021-01-15 08:20

How can I refer to a class from a static method without using the class name itself in JavaScript (similar to PHP\'s self and self::method_name)?<

3条回答
  •  自闭症患者
    2021-01-15 08:52

    If all of those methods are static then you can use this.

    class FooBar {
        static foo() {
            return 'foo';
        }
    
        static bar() {
            return 'bar';
        }
    
        static foobar() {
            return this.foo() + this.bar();
        }
    }
    

提交回复
热议问题