array.length 和 array.prototype.length 有什么区别?
MDN says它们在功能上应该是等效的。有什么不同?我什么时候应该使用其中一种而不是另一种?为什么后者给我一个错误?
var a = [1,2,3,4,5];
console.log(a.length);//5
console.log(a.prototype.length);// Uncaught TypeError: Cannot read property 'length' of undefined
最佳答案
Array.prototype是一个数组,其长度初始设置为 0。
数组实例有自己的、特殊的、 self 调整的length属性,所以当你这样做时:
var a = [];
console.log(a.length);
您看到的是a的length属性的值,它不是从其[[Prototype]]
继承的。当你这样做时:
console.log(Array.prototype.length);
您将看到 Array.prototype 数组的长度,它是一个具有自己的 length 属性的不同对象。更改 Array 实例的长度不会影响 Array.prototype 的长度,反之亦然。
关于javascript - array.prototype.length 给我一个错误是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27306633/