bind-decorator icon indicating copy to clipboard operation
bind-decorator copied to clipboard

Decorated functions cannot be overwritten

Open nostalgiaz opened this issue 5 years ago • 3 comments

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

nostalgiaz avatar Nov 26 '18 14:11 nostalgiaz

Got the same issue

Krakabek avatar May 07 '20 08:05 Krakabek

Same here +1

silkimen avatar Aug 04 '20 16:08 silkimen

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

gosimowicz avatar Dec 14 '22 09:12 gosimowicz