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(); // error
However, DataView
can be subclassed using the extends
syntax:
The
DataView
constructor is designed to be subclassable. It may be used as the value of anextends
clause of a class definition. Subclass constructors that intend to inherit the specifiedDataView
behaviour must include asuper
call to theDataView
constructor to create and initialize subclass instances with the internal state necessary to support theDataView.prototype
built-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"