0

AngularJs Decorators Provides an effective way to extend functions in services without affecting the original service. How does one emulate this behavior with javascript classes?

I have 2 libraries. One containing common logic and UI elements and another more specific to the page I am building. I have to extend the class written in the library containing the core logic in my specific one and then use the extended version's functions from the core library. I do understand this might seem like a faulty design, but the same could be achieved with decorators in AngularJS.However, I am using plain ES6 Classes. So how do I go about it?

Zer0
  • 448
  • 1
  • 5
  • 16

1 Answers1

0

Rather silly example:

class A {foo() {console.log(1)}}
var a  = new A();
// super decorate
var t  = a.foo; a.foo = () => { console.log(2); t()}
// awesome result:
a.foo()
>> 2
>> 1
Petr Averyanov
  • 9,327
  • 3
  • 20
  • 38