Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subclassing is hard #65

Open
pocesar opened this issue Jan 18, 2016 · 5 comments
Open

Subclassing is hard #65

pocesar opened this issue Jan 18, 2016 · 5 comments

Comments

@pocesar
Copy link

pocesar commented Jan 18, 2016

Since all the 'static' functions (like concat, wrap, etc) use the ByteBuffer definition itself instead of "this", it's hard to subclass it. By using this.constructor and this (in static) instead of forcefully deriving from ByteBuffer, it can work. I can submit a patch, and I think it won't break anything, but will make it easier to add methods to the class without monkeypatching it

@pocesar
Copy link
Author

pocesar commented Jan 23, 2016

export class BB extends ByteBuffer {

    static allocate( capacity?: number, littleEndian?: number, noAssert?: boolean ): BB {
        var c: BB = <any>super.allocate(capacity, littleEndian, noAssert);
        c.readBytes = this.prototype.readBytes;
        c.refresh = this.prototype.refresh;
        return c;
    }

    static wrap( buffer: ByteBuffer | Buffer | ArrayBuffer | Uint8Array | string | Array<number>, enc?: string | boolean, littleEndian?: boolean, noAssert?: boolean ): BB {
        var c: BB = <any>super.wrap(buffer, enc, littleEndian, noAssert);
        c.readBytes = this.prototype.readBytes;
        c.refresh = this.prototype.refresh;
        return c;
    }

    static concat( buffers: Array<ByteBuffer | Buffer |  ArrayBuffer | Uint8Array | string>, encoding?: string | boolean, litteEndian?: boolean, noAssert?: boolean ): BB {
        var c: BB = <any>super.concat(buffers, encoding, litteEndian, noAssert);
        c.readBytes = this.prototype.readBytes;
        c.refresh = this.prototype.refresh;
        return c;
    }

}

@dcodeIO
Copy link
Member

dcodeIO commented Jan 23, 2016

So, what you are proposing basically is to change https://github.com/dcodeIO/bytebuffer.js/blob/master/src/methods/static/allocate.js#L12 and similar to:

return new this(capacity, littleEndian, noAssert);

Correct?

@pocesar
Copy link
Author

pocesar commented Jan 24, 2016

Yup, exactly that, so it can be inherited from without those hacks. and when instance methods like clone, copy that returns a new ByteBuffer, should be new this.constructor()

@dcodeIO
Copy link
Member

dcodeIO commented Jan 24, 2016

So, is this pattern safe to use across (not so modern) browsers?

@pocesar
Copy link
Author

pocesar commented Jan 24, 2016

Yup, you can check in built-in classes, like [].constructor, ({}).constructor, (0).constructor,(function(){}).constructor and so on, it's from the first version of JS (1.1ish I think)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants