【问题标题】:Why do TypedArrays have a static length property with value 3为什么 TypedArrays 有一个值为 3 的静态长度属性
【发布时间】:2020-03-30 13:46:18
【问题描述】:

我在查找Uint8Array 的一些属性时遇到了this gem

TypedArray.length

    值为 3 的长度属性。

我试过了,是真的!

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

【问题讨论】:

  • 在控制台中运行:var x = new Uint8Array([17, -45.3]); console.log(x.length);
  • 哈我知道有人会认为我在问如何获得正确的长度。这不是我要问的。

标签: javascript typed-arrays uint8array


【解决方案1】:

对于函数,长度属性是其参数列表中有多少个参数。对于 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');

【讨论】:

  • 啊好吧,这有点模糊!
猜你喜欢
  • 2013-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多