Check for an instance of ArrayBufferView?

前端 未结 2 2116
被撕碎了的回忆
被撕碎了的回忆 2021-02-15 09:24

Background

With a bit of research I\'ve found that, although ArrayBufferView wasn\'t initially exposed (through [NoInterfaceObject]) there appeared to be

相关标签:
2条回答
  • 2021-02-15 10:09

    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;
    }
    
    0 讨论(0)
  • 2021-02-15 10:12

    Better answer I guess:

    var arr = new Float64Array(100);
    
    arr instanceof (new Uint16Array()).constructor.prototype.__proto__.constructor //true
    

    works in Chrome & Firefox, maybe other browsers too

    0 讨论(0)
提交回复
热议问题