ndugger
ndugger

Reputation: 7551

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.

Upvotes: 2

Views: 401

Answers (1)

Buzinas
Buzinas

Reputation: 11733

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);
}

Upvotes: 4

Related Questions