Is this a problem in the real world? Most people understand that simply overriding methods can break functionality. Therefore, doesn't the simple approach I gave suffice for the requirement in at least most cases? Are you able to describe a scenario in which we really need to protect people who are overriding a method from breaking functionality that they might not even expect to occur after they've overridden it?
On Fri, 19 Jul 2019 at 09:32, Michael Haufe <t...@thenewobjective.com> wrote: > Assuming your base class: > > > > <script> > > class Base { > > methodWIthAfter(){ > > method() > > after() > > } > > } > > </script> > > > > If I override the method, there is no guarantee that I called > `super.methodWithAfter()`, so `after()` is never executed. > > > > <script> > > class A extends Base { > > methodWithAfter() { > > method2() > > } > > } > > </script> > > > > Additionally, requiring me to call ` super.methodWithAfter()` is an > Anti-Pattern: https://en.wikipedia.org/wiki/Call_super > > > > /Michael > > > > *From:* Naveen Chawla <naveen.c...@gmail.com> > *Sent:* Friday, July 19, 2019 3:24 AM > *To:* Michael Haufe <t...@thenewobjective.com> > *Cc:* j...@trusktr.io; es-discuss <es-discuss@mozilla.org> > *Subject:* Re: A way to fire logic at the end of completion of the > current class method (regardless of super call order). > > > > Can anyone tell me what's wrong with a really simple approach? Wherever > you are calling method(), just call methodWithAfter(): > > > > //in base class > > methodWithAfter(){ > > method(); > > after() > > } > > > > What am I missing, guys? > > > > On Fri, 19 Jul 2019 at 07:43, Michael Haufe <t...@thenewobjective.com> > wrote: > > Revisiting this topic: why is the Template Method pattern not acceptable > to accomplish this? > > <script> > class Base { > constructor() { > this._beforeAction() > this._action() > this._afterAction() > } > _beforeAction(){ > console.log(`Base._beforeAction()`) > } > _action(){ > console.log(`Base._action()`) > } > _afterAction(){ > console.log(`Base._afterAction()`) > } > } > > class A extends Base { > _action(){ > console.log(`A._action()`) > } > } > > let a = new A() > // console reads: > // > Base._beforeAction() > // > A._action() > // > Base._afterAction() > </script> > > /Michael > ----- > > Monday, February 11, 2019 10:34 PM Michael Haufe <t...@thenewobjective.com> > wrote: > > > You can use a Proxy in the base class: > > > > <script> > > let handlerExample = { > > get(target, prop) { > > let feature = target[prop] > > return feature instanceof Function ? > > function () { > > console.log('Before call'); > > let result = feature.apply(this, arguments) > > console.log('After call'); > > return result > > } > > : feature > > } > > } > > > > class Base { > > constructor() { > > return new Proxy(this, handlerExample) > > } > > m1() { return "m1" } > > } > > > > class Sub extends Base { > > m1() { return `override ${super.m1()}` } > > m2() { return `m2` } > > } > > > > let base = new Base() > > console.log(base.m1()) > > > > let sub = new Sub() > > console.log(sub.m1()) > > console.log(sub.m2()) > > </script> > > > > /Michael > > > On Fri, Feb 8, 2019 at 1:22 AM #!/JoePea <j...@trusktr.io> wrote: > > > I many times find myself in cases where a base class wants to ensure > that logic is always fired after the current method's execution, so that > for example no matter in which order sub classes call the `super` method, > the `super` method can still guarantee that logic fires after the whole > stack of the same method in the class hierarchy. > > > So what I can do now is use `Promise.resolve().then(() => { ... })` to > schedule that logic for later, that way all the invocations of a `foo` > method along the class hierarchy have all fired. But this means that other > code can also fire before the next microtask. > > > Is there some way to do it? If not, I wonder if some language feature > for doing it would be possible? > _______________________________________________ > es-discuss mailing list > es-discuss@mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > >
_______________________________________________ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss