Skip to content Skip to sidebar Skip to footer

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 an extends clause of a class definition. Subclass constructors that intend to inherit the specified DataView behaviour must include a super call to the DataView constructor to create and initialize subclass instances with the internal state necessary to support the DataView.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"