NoHomey / bind-decorator

The fastest automatic method.bind(this) decorator
MIT License
68 stars 11 forks source link

Decorated functions cannot be overwritten #11

Open nostalgiaz opened 5 years ago

nostalgiaz commented 5 years ago

This is what i mean

describe('when used to bind to this context', function () {
    class thisContext {
        protected test: string;

        public constructor() { }

        public getTest(): string {
            return this.test;
        }

        @bind
        public setTest(test: string): void {
            this.test = test;
        }
    }

    it('binds decorated method to this context', function () {
        const tested: thisContext = new thisContext();
        const { setTest } = tested;
        setTest('unit');

        expect(tested.getTest()).toBe('unit');
    });

    it('can be overwritten as well', function () {
        class thisInheritedContext extends thisContext {
            @bind
            public setTest(test: string): void {
                this.test = 'inherited ' + test;
            }
        }

        const tested: thisContext = new thisContext();
        tested.setTest('unit');
        expect(tested.getTest()).toBe('unit');

        const inheritedTested: thisInheritedContext = new thisInheritedContext();
        inheritedTested.setTest('unit');
        expect(inheritedTested.getTest()).toBe('inherited unit');
    });
});
Krakabek commented 4 years ago

Got the same issue

silkimen commented 4 years ago

Same here +1

gosimowicz commented 1 year ago

@nostalgiaz @Krakabek @silkimen If still needed https://github.com/NoHomey/bind-decorator/pull/13