Looking to extend the functionality of the DataView object by creating a custom type
and adding extra methods, but unsure of the correct approach. Attempted the following:
var CustomDataView = function() {
this.offset = 0;
};
CustomDataView.prototype.__proto__ = DataView.prototype;
CustomDataView.prototype.readU8 = function() {
if (this.byteLength >= this.offset+1) {
return this.getUint8(this.offset++);
} else {
return null;
}
};
Encountered an error:
DataView.prototype.byteLength called on incompatible receiver CustomDataView
Exploring alternate approaches, tried the following:
var CustomDataView = function CustomDataView(buffer, byteOffset, byteLength) {
DataView.call(this, buffer, byteOffset, byteLength);
this.offset = 0;
};
CustomDataView.prototype = Object.create(DataView.prototype);
CustomDataView.prototype.constructor = CustomDataView;
Received an error:
TypeError: Constructor DataView requires 'new'