Array.size() vs Array.length

后端 未结 10 824
一个人的身影
一个人的身影 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 02:08

    Actually, .size() is not pure JavaScript method, there is a accessor property .size of Set object that is a little looks like .size() but it is not a function method, just as I said, it is an accessor property of a Set object to show the unique number of (unique) elements in a Set object.

    The size accessor property returns the number of (unique) elements in a Set object.

    const set1 = new Set();
    const object1 = new Object();
    
    set1.add(42);
    set1.add('forty two');
    set1.add('forty two');
    set1.add(object1);
    
    console.log(set1.size);
    // expected output: 3
    

    And length is a property of an iterable object(array) that returns the number of elements in that array. The value is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array.

    const clothing = ['shoes', 'shirts', 'socks', 'sweaters'];
    
    console.log(clothing.length);
    // expected output: 4
    

提交回复
热议问题