javascript-decorators icon indicating copy to clipboard operation
javascript-decorators copied to clipboard

Decorators and inheritance

Open miguelcobain opened this issue 9 years ago • 8 comments

How would decorators play along with class inheritance? Would it be worth mentioning it in the document?

miguelcobain avatar Apr 21 '15 10:04 miguelcobain

What's ambiguous with decorators and how inheritance is handled?

sebmck avatar Apr 21 '15 10:04 sebmck

Example: when we override a method, do we expect it to inherit the superclasses method decorators?

class Animal {
  @transport
  move() {
  }
}

class Cat extends Animal {
  move() { //has this method @transport decorator?
  }
}

I can provide a more sensible example if needed.

miguelcobain avatar Apr 21 '15 10:04 miguelcobain

The new descriptor would shadow the super one. Descriptors can be modified (it's the point of decorators) so it's impossible to track for inheritance.

sebmck avatar Apr 21 '15 10:04 sebmck

It would be good to have the super class decorators applied to the concrete bottommost method, "incrementally" changing the descriptor with all decorators. Is this nonsense?

My knowledge in descriptors is a bit limited, that's why I shouldn't talk too much about decorators. :smile:

miguelcobain avatar Apr 21 '15 10:04 miguelcobain

You can't track where the original value is so you can't inherit descriptors.

function foo(target, name, descriptor) {
  var value = descriptor.value;
  descriptor.value = function () {
    return "whatever"; // we could do something with `value`
  };
}

class Foo {
  @foo
  foo() {}
}

You have no idea where the original method is and how it's going to be used so you can't swap it out.

sebmck avatar Apr 21 '15 10:04 sebmck

You mean that from the @foo decorator implementation we can't access the original foo() {} class function?

miguelcobain avatar Apr 21 '15 10:04 miguelcobain

You can't track where the original method is so you can't "replace" it.

sebmck avatar Apr 21 '15 10:04 sebmck

Close, invalid?

silkentrance avatar Feb 28 '16 23:02 silkentrance