JavaScript 中 array.size() 和 array.length 的区别
array.size() 方法在功能上等同于array.length 属性,它只能在jQuery中使用。在 JavaScript 中array.size()是无效的方法,所以应该使用array.length 属性。下面的例子实现了上述概念:
示例 1:此示例演示了 array.size() 方法和 array.length 属性。
html
Difference between array.size() method
and array.length property
Click the button to display
the length of array.
Length of the array is:
html
Array.length property
Click the button to display
the length of array.
Length of array the is:
html
array.length property with
non-numeric index key
Click the button to display
the length of array.
Length of array the is:
输出:
Length of the array is: 3
error on console: TypeError: arr.size is not a function
注意:对于具有数字索引值的数组, array.length 属性返回 last_key+1 的值。此属性不保证找到数组中的项目数。
示例 2:此示例显示 Array.length 属性的工作原理。
html
Array.length property
Click the button to display
the length of array.
Length of array the is:
输出:
Length of the array is: 51
示例 3:此示例显示当索引键为非数字时 array.length 属性如何工作。
html
array.length property with
non-numeric index key
Click the button to display
the length of array.
Length of array the is:
输出:
Length of the array is: 0
让我们以表格形式查看差异 - : Its syntax is -: array.size() Its syntax is -: array.length Its supported browsers are -: Chrome , Internet Explorer, Firefox, Safari, Opera Microsoft Edge Its supported browsers are -: Chrome , Internet Explorer, Firefox, Safari, Opera, Microsoft Edge array.size() array.length() 1. This property returns the size of the array This property returns the size of the array 2. 3. It cannot be used in Javascript but only used in Jquery It is used in Javascript, not in jQuery 4. This is a feature of jQuery array.length is an ECMAScript1 feature. 5