I would have naively written:
this.getByteArray = function() {
return data.subarray(0, length);
};
What would be wrong about that?