为什么 TypedArrays 具有值为 3 的静态长度属性

Tim*_*mmm 1 javascript typed-arrays uint8array

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

TypedArray.length

    值为 3 的长度属性。

我试过了,是真的!

武侠

什么?为什么会存在这个?!

Nic*_*wer 5

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

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

console.log(example2.length);
console.log(example3.length);
Run Code Online (Sandbox Code Playgroud)

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

// 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');
Run Code Online (Sandbox Code Playgroud)