简体   繁体   English

Babel.js方法修饰符应用于类而不是方法

[英]Babel.js method decorators applying to class instead of method

While attempting to apply a decorator to a class' method, It's seemingly applying it to the class instead. 尝试将装饰器应用于类的方法时,似乎正在将其应用于类。 Me, being not all that familiar with decorators/annotations, It's more than likely user-error. 我,不是很熟悉装饰器/注释,这很可能是用户错误。

Here's a really quick example that I've whipped up: 这是我提起的一个非常简单的例子:

class Decorators {
    static x (y, z) {
        return method => {
            // do stuff with y, z, method
            // method should be the method that I decorate
        }
    }
}

class Foo {
    @Decorators.x('foo', 'bar')
    static main () {
        // ...
    }
}

As you can see, inside of the decorator, the method should be equal to the static main method, but when I add a console.log to the decorator to see what the method is, it logs [Function: Foo] (which is the Foo class after transpilation via Babel)... 如您所见,在装饰器内部,该方法应等于static main方法,但是当我向装饰器添加console.log以查看该方法是什么时,它将记录[Function: Foo] (通过Babel进行翻译后的Foo课)...

From what I can tell, Babel.js is applying the decorator to the class, even though it's set on the method. 据我所知,Babel.js会将装饰器应用于类,即使它是在方法上设置的也是如此。 I could be wrong, though. 我可能是错的。

The first parameter is the target (it can be the class constructor - for statics, the prototype - for non-statics, and the instance, in case of properties). 第一个参数是target (对于属性,它可以是类构造函数-对于静态对象,原型-对于非静态对象,以及实例)。 And the second one is the method's name: 第二个是方法的名称:

return (target, name, descriptor) => {
  console.log(name);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM