當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Javascript array.size()和array.length的區別用法及代碼示例


數組.size()方法在函數上等同於Array length它隻能用於jQuery。在 JavaScript 中數組.size()是無效方法所以array.length 屬性應該使用。下麵的例子實現了上述概念:

示例 1:此示例演示 array.size() 方法和 array.length 屬性。

html


<!DOCTYPE html>
<html>
<head>
    <title>
        Difference between array.size() method
        and array.length property
    </title>
</head>
<body>
     
<p>
        Click the button to display
        the length of array.
    </p>
    <button onclick="findLength()">
        Try it
    </button>
     
<p>
        Length of the array is:
        <span id="demo"></span>
    </p>
    <script>
        var arr = ['geeks', 'for', 'geeks'];
        function findLength() {
            document.getElementById("demo").innerHTML
                        = arr.length;
             
            document.getElementById("demo").innerHTML
                        = arr.size();
        }
    </script>
</body>
</html>

輸出:

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


<!DOCTYPE html>
<html>
<head>
    <title>
        Array.length property
    </title>
</head>
<body>
     
<p>
        Click the button to display
        the length of array.
    </p>
    <button onclick="findLength()">
        Try it
    </button>
     
<p>
        Length of array the is:
        <span id="demo"></span>
    </p>
    <script>
        var arr = ['geeks', 'for', 'geeks'];
        arr[50] = 'article';
         
        function findLength() {
            document.getElementById("demo")
                        .innerHTML = arr.length;
        }
    </script>
</body>
</html>

輸出:

Length of the array is: 51

示例 3:此示例顯示當索引鍵為非數字時array.length 屬性如何工作。

html


<!DOCTYPE html>
<html>
<head>
    <title>
        array.length property with
        non-numeric index key
    </title>
</head>
<body>
     
<p>
        Click the button to display
        the length of array.
    </p>
    <button onclick="findLength()">
        Try it
    </button>
     
<p>
        Length of array the is:
        <span id="demo"></span>
    </p>
    <script>
        var arr = new Array();
        arr['a'] = 1;
        arr['b'] = 2;
        arr['c'] = 3;
         
        function findLength() {
            document.getElementById("demo")
                    .innerHTML = arr.length;
        }
    </script>
</body>
</html>

輸出:

Length of the array is: 0

讓我們以表格形式看看差異:

數組.size() 數組.length()
1. 該屬性返回數組的大小 該屬性返回數組的大小
2.

它的語法是-:

數組.size()

它的語法是-:

array.length

3. 不能在Javascript中使用,隻能在Jquery中使用 它用於 Javascript,而不是 jQuery
4. 這是jQuery的一個特性 array.length 是 ECMAScript1 函數。
5

它支持的瀏覽器是 -:

Chrome、Internet Explorer、Firefox、Safari、Opera Microsoft Edge

它支持的瀏覽器是 -:

Chrome、Internet Explorer、Firefox、Safari、Opera、Microsoft Edge



相關用法


注:本文由純淨天空篩選整理自karthi03大神的英文原創作品 Difference between array.size() and array.length in JavaScript。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。