Check instance of ArrayBufferView?

Background

With a bit of research, I found that although ArrayBufferView not initially detected (via [NoInterfaceObject]), there seemed to be widespread agreement that this should be due to my described use case.

The original convention was to expose the ArrayBufferView constructor in the ArrayBufferView namespace, which was implemented in Safari (and still works in 6.1.1) and Chrome, but was then pulled from Chrome in favor of the static method ArrayBuffer.isView() .

Meanwhile, Mozilla (still) talks about implementing ArrayBuffer.isView() .

In short:

  • Safari provides an ArrayBufferView constructor

  • Chrome has ArrayBuffer.isView()

  • There is nothing in Firefox

  • IE - I haven't even come close ...

Question

So my question. What is the most concise way to check if an object is an instance of ArrayBufferView?

+7
javascript instanceof arraybuffer typedarray
source share
2 answers

I would use either:

 function isAbv(value) { return value && value.buffer instanceof ArrayBuffer && value.byteLength !== undefined; } 

or

 var ArrayBufferView = Object.getPrototypeOf(Object.getPrototypeOf(new Uint8Array)).constructor; function isAbv(value) { return value instanceof ArrayBufferView; } 
+4
source share

The best answer, I think:

var arr = new Float64Array (100);

 arr instanceof (new Uint16Array()).constructor.prototype.__proto__.constructor //true 

works in Chrome and Firefox, maybe other browsers too

+1
source share

All Articles