Array.size() vs Array.length

后端 未结 10 844
一个人的身影
一个人的身影 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条回答
  •  旧时难觅i
    2020-11-28 01:58

    The property 'length' returns the (last_key + 1) for arrays with numeric keys:

    var  nums  =  new  Array ();
    nums [ 10 ]  =  10 ; 
    nums [ 11 ]  =  11 ;
    log.info( nums.length );
    

    will print 12!

    This will work:

    var  nums  =  new  Array ();
    nums [ 10 ]  =  10 ; 
    nums [ 11 ]  =  11 ;
    nums [ 12 ]  =  12 ;
    log.info( nums.length + '  /  '+ Object.keys(nums).length );
    

提交回复
热议问题