-
Notifications
You must be signed in to change notification settings - Fork 155
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
Comments
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;
}
} |
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:
Correct? |
Yup, exactly that, so it can be inherited from without those hacks. and when instance methods like |
So, is this pattern safe to use across (not so modern) browsers? |
Yup, you can check in built-in classes, like |
Since all the 'static' functions (like
concat
,wrap
, etc) use the ByteBuffer definition itself instead of "this", it's hard to subclass it. By usingthis.constructor
andthis
(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 itThe text was updated successfully, but these errors were encountered: