如何找到 typedArray 的类型?

How to find the type of a typedArray?

判断一个对象是否为数组,有一个方法-

const myarray = [1,2,3,4]; 
Array.isArray(myarray); 
//returns true

类型数组(例如 Uint8Array)是否有类似的方法?

const int8 = new Uint8Array([0,1,2,3]);
Array.isArray(int8); //returns false

如何判断对象是否为类型化数组,以及类型化数组的类型?

可以用.name属性就可以了。它应该 return 类型:"Uint8Array"。这也适用于 Uint16 和 Unint32。

MDN:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/name

编辑:添加来源

你可以用它的构造函数进行比较1:

const int8 = new Uint8Array([0,1,2,3]);
console.log(int8.constructor === Uint8Array);

// so you can do
function checkTypedArrayType(someTypedArray) {
  const typedArrayTypes = [
    Int8Array,
    Uint8Array,
    Uint8ClampedArray,
    Int16Array,
    Uint16Array,
    Int32Array,
    Uint32Array,
    Float32Array,
    Float64Array,
    BigInt64Array,
    BigUint64Array
  ];
  const checked = typedArrayTypes.filter(ta => someTypedArray.constructor === ta);
  return checked.length && checked[0].name || null;
}

console.log(checkTypedArrayType(int8));

// but this can be hugely simplified
function checkTypedArrayType2(someTypedArray) {
  return someTypedArray && 
    someTypedArray.constructor && 
    someTypedArray.constructor.name || 
    null;
}

console.log(checkTypedArrayType2(int8));

// which can actually be generalized to
const whatsMyType = someObject => 
  someObject && 
    someObject.constructor && 
    someObject.constructor.name && 
    someObject.constructor.name || 
    null;

console.log(whatsMyType(int8));
console.log(whatsMyType([1,2,3,4]));
console.log(whatsMyType("hello!"))
console.log(whatsMyType(/[a-z]/i))

1 请注意,没有任何版本的 Internet Explorer 支持 name 属性。 See MDN. See also

对于 TypedArrays,实际上有一个等价于 Array.isArray 的东西,可以在 ArrayBuffer 构造函数上访问:

ArrayBuffer.isView

但是请注意,对于 DataView 对象,它也会 return 为真,因此如果您真的想知道一个对象是否是 TypedArray,您可以简单地执行以下操作:

function isTypedArray( arr ) {
  return ArrayBuffer.isView( arr ) && !(arr instanceof DataView);
}

function test( val ) {
  console.log( isTypedArray( val ) );
}

test( false ); // false
test( null ); // false
test( [] ); // false
test( new ArrayBuffer( 12 ) ); // false
test( new DataView( new ArrayBuffer( 12 ) ) ); // false
test( new Uint8Array( 12 ) ); // true;
test( new BigInt64Array( 12 ) ); // true;

但这不会为您提供该 TypedArray 的特定类型。

TypedArray 的原型公开了一个 BYTES_PER_ELEMENT 属性,但仅此而已......

构造函数上有一个可访问的.name property,它本身可以通过每个实例的.constructor 属性访问,但可以设置,IE不支持。但是,如果这对您来说不是问题,则只需:

function getType( arr ) {
  return isTypedArray( arr ) && arr.constructor.name;
}
function isTypedArray( arr ) {
  return ArrayBuffer.isView( arr ) && !(arr instanceof DataView);
}

function test( val ) {
  console.log( getType( val ) );
}

test( [] ); // false
test( new ArrayBuffer( 12 ) ); // false
test( new DataView( new ArrayBuffer( 12 ) ) ); // false
test( new Uint8Array( 12 ) ); // "Uint8Array";
test( new BigInt64Array( 12 ) ); // "BigInt64Array";

如果您需要支持 IE,使用 instanceof 独立测试所有可能的 TypedArrays 构造函数也是一种选择:

const typedArrays = [
 'Int8',
 'Uint8',
 'Uint8Clamped',
 'Int16',
 'Uint16',
 'Int32',
 'Uint32',
 'Float32',
 'Float64',
 'BigInt64',
 'BigUint64'
].map( (pre) => pre + 'Array' );

function getType( arr ) {
  return isTypedArray( arr ) &&
    typedArrays.find( (type) => arr instanceof window[ type ] );
}

function isTypedArray( arr ) {
  return ArrayBuffer.isView( arr ) && !(arr instanceof DataView);
}

function test( val ) {
  console.log( getType( val ) );
}

test( [] ); // false
test( new Uint8Array( 12 ) ); // Uint8Array
test( new Int16Array( 12 ) ); // Int16Array
test( new Float32Array( 12 ) ); // Float32Array

另一种方法是,您可以使用类型化数组 BYTES_PER_ELEMENT 属性

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/BYTES_PER_ELEMENT

Array.isTypedArray = function(inArray) {
  if (inArray) {
    const prototype = Object.getPrototypeOf(inArray);
    return prototype ? prototype.hasOwnProperty("BYTES_PER_ELEMENT") : false;
  }
  return false;
};

如其他答案中所述,要获得实际类型,您可以使用 constructor.name。重用上面的isTypedArray函数,你可以这样写。

function getType(obj){
    return Array.isTypedArray(obj) ? obj.constructor.name: (typeof obj)
}

示例代码 & console.logs

Array.isTypedArray = function(inArray){
    if(inArray){
        const prototype = Object.getPrototypeOf(inArray);
        return prototype ? prototype.hasOwnProperty("BYTES_PER_ELEMENT") : false;
    }
    return false;
}

console.log(" 'Iam' a typed array  ", Array.isTypedArray([1,2,3]));
console.log("[1,2,3] is typed array  ", Array.isTypedArray([1,2,3]));
console.log("new Int8Array([1,2,3]) is typed array  ", Array.isTypedArray(new Int8Array([1,2,3])));
console.log("new BigUint64Array() is typed array  ", Array.isTypedArray(new BigUint64Array()));
console.log("new Uint8ClampedArray([1,2,3]) is typed array  ", Array.isTypedArray(new Uint8ClampedArray([1,2,3])));
console.log("new Float32Array([1,2,3]) is typed array  ", Array.isTypedArray(new Float32Array([1,2,3])));
console.log("new BigUint64Array() is typed array  ", Array.isTypedArray(new BigUint64Array()));
console.log("new Set() is typed array  ", Array.isTypedArray(new Set()));
console.log("null is typed array  ", Array.isTypedArray(null));
console.log("undedined is typed array  ", Array.isTypedArray(undefined));
console.log("{} is typed array  ",Array.isTypedArray({}));


function getType(obj){
    return Array.isTypedArray(obj) ? obj.constructor.name: (typeof obj)
}

console.log("Type of Array  ", getType(new Array()));
console.log("Type of Int8Array  ", getType(new Int8Array()));
console.log("Type of Uint8Array  ",getType( new Uint8Array()));
console.log("Type of Uint8ClampedArray  ",getType( new Uint8ClampedArray()));
console.log("Type of Int16Array  ", getType(new Int16Array()));
console.log("Type of Float32Array  ", getType(new Float32Array()));
console.log("Type of BigInt64Array  ", getType(new BigInt64Array()));
console.log("Type of BigUint64Array  ", getType(new BigUint64Array()));