首页 > 解决方案 > 为什么 TypedArrays 有一个值为 3 的静态长度属性

问题描述

我只是在查找一些属性时遇到了这个宝石:Uint8Array

TypedArray.length

    值为 3 的长度属性。

我试过了,这是真的!

wtf

什么?为什么会有这个?!

标签: javascripttyped-arraysuint8array

解决方案


对于函数,长度属性是其参数列表中有多少个参数。对于 Uint8Array 构造函数,该数字为 3。

function example2 (a, b) {}
function example3 (a, b, c) {}

console.log(example2.length);
console.log(example3.length);

无论长度属性如何,任何函数都可以传递任意数量的参数,并且该函数可以使用或忽略所有参数。所以长度只是暗示可能使用多少个。

// This function doesn't list any arguments, so it's length is 0
function example () {
  // ...but it uses 2 anyway.
  console.log(arguments[0], arguments[1])
}
console.log(example.length);
// .. and i can pass in more than 2, useless though it is.
example('first', 'second', 'third');


推荐阅读