javascript - Why is the value of Float32Array.length always 3? - Stack Overflow

I was reading the Mozilla Developer Network docs on Float32Arrays when I came uponFloat32Array.lengthL

I was reading the Mozilla Developer Network docs on Float32Arrays when I came upon

Float32Array.length
Length property whose value is 3.

... why is always 3? I also noticed that prototype property of the same name overrides it.

I was reading the Mozilla Developer Network docs on Float32Arrays when I came upon

Float32Array.length
Length property whose value is 3.

... why is always 3? I also noticed that prototype property of the same name overrides it.

Share Improve this question edited Apr 5, 2015 at 11:58 Gumbo 656k112 gold badges791 silver badges851 bronze badges asked Apr 5, 2015 at 11:53 John HoffmanJohn Hoffman 18.7k21 gold badges60 silver badges84 bronze badges 2
  • 2 I'd nearly think this is a religious question – wvdz Commented Apr 5, 2015 at 11:54
  • @popovitsj I think he meant 42. – β.εηοιτ.βε Commented Apr 5, 2015 at 11:55
Add a ment  | 

3 Answers 3

Reset to default 7

Float32Array is actually a function. You can check that like this

console.assert(typeof Float32Array === 'function');

And that function accepts three parameters. Quoting the signature from the same documentation,

Float32Array(buffer [, byteOffset [, length]]);

Quoting the Function.length documentation,

length is a property of a function object, and indicates how many arguments the function expects, i.e. the number of formal parameters.

That is why the length property of Float32Array is always 3.

It's because the constructor takes up to 3 arguments:

Float32Array(buffer [, byteOffset [, length]]);

Every function in JavaScript has a length property that will return the count of the named parameters it takes.

E.g.

function foo(a, b) {}
foo.length === 2; // true

function bar() {}
bar.length === 0; // true

This is the length of number of parameters for the (object-) function Float32Array.

However, when you instantiate it length will represent number of indexes:

console.log(Float32Array.length);  // => 3, number of arguments

var a = new Float32Array(10);      // create an instance with 10 indexes
console.log(a.length);             // => 10, number of indexes

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744129891a4559791.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信