Array.size() vs Array.length

后端 未结 10 832
一个人的身影
一个人的身影 2020-11-28 01:17

What is the difference between the two?

So I know that array.size() is a function while array.length is a property. Is there a usecase for

10条回答
  •  眼角桃花
    2020-11-28 01:57

    The .size() function is available in Jquery and many other libraries.

    The .length property works only when the index is an integer.

    The length property will work with this type of array:

    var nums = new Array();
    nums[0] = 1; 
    nums[1] = 2;
    print(nums.length); // displays 2
    

    The length property won't work with this type of array:

    var pbook = new Array(); 
    pbook["David"] = 1; 
    pbook["Jennifer"] = 2;
    print(pbook.length); // displays 0
    

    So in your case you should be using the .length property.

提交回复
热议问题