Dataview And Prototypical Inheritance
From what I've gleamed online, one way to extend an object in JavaScript is by first cloning it's prototype, then setting that prototype as the prototype of the subclass. It doesn'
Solution 1:
Not all constructors allow you to call them, e.g. ES6 classes:
class Foo {}
new Foo(); // OKFoo(); // error
Foo.call(); // errorHowever, DataView can be subclassed using the extends syntax:
The
DataViewconstructor is designed to be subclassable. It may be used as the value of anextendsclause of a class definition. Subclass constructors that intend to inherit the specifiedDataViewbehaviour must include asupercall to theDataViewconstructor to create and initialize subclass instances with the internal state necessary to support theDataView.prototypebuilt-in methods.
classPacketextendsDataView {
constructor(opcode, size) {
super(newArrayBuffer(size));
this.setInt8(0, opcode);
}
send (websocket) {
// Send packet here ...
}
}
var packet = newPacket(0, 5);
Post a Comment for "Dataview And Prototypical Inheritance"