简体   繁体   中英

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)...

From what I can tell, Babel.js is applying the decorator to the class, even though it's set on the method. 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). And the second one is the method's name:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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