Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #12270

Re: Empty array

From Asen Bozhilov <asen.bozhilov@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: Empty array
Date 2012-02-29 01:23 -0800
Organization http://groups.google.com
Message-ID <3762265.125.1330507383456.JavaMail.geo-discussion-forums@ynil17> (permalink)
References <0f481e7c-97b9-4c76-9660-321681876658@w9g2000vbv.googlegroups.com> <e8bddc62-6b7f-4463-b0bb-51912eeca4ef@z31g2000vbt.googlegroups.com> <CESvYCJ95UTPFw+M@invalid.uk.co.demon.merlyn.invalid> <opuqk7hsm5bkmp0b14shc14vmv3di8honh@4ax.com>

Show all headers | View raw


Gene Wirchenko написа:

>      The emptiness check could apparently be
> 
> var fEmpty=true;
> for (Item in theArray)
>   {
>   fEmpty=false;
>   break;
>   }
> 
> I checked this, and it works, but is there a better way?  Entering a
> loop and breaking out of it unconditionally is perverse.

Actually this is not a check for an emptiness. That checks for enumerable properties, which is different than the OP's question.   

Firstable we have to consider what is an empty object in ECMAScript. By default every native object inherits from `Object.prototype` and the object is not empty.  The only one exception is: 

var empty = Object.create(null);  

With `Object.create` you can create proto-less object, which does not inherit properties from anywhere. That object can be consider as an empty object.

ECMAScript 5 provides API for manipulating the internal states of the properties. We are able to create non-enumerable properties, which means the checking for emptiness with `for-in` will not return correct result.  

As I wrote, there are no reasons to use sparse arrays or arrays with no integer properties. In both cases a native object would be sufficient. The arrays are usable when they are sequenced collection. 
When I expect somewhere an array in my program I do the following to extract its elements. 

for (var i = 0, len = arr.length; i < len; i++) {
    // do something with arr[i] element
} 

By this, checking for emptiness of array is achieved via its `length' property. This is the simpliest and efficient way. 

Checking for emptiness of sparse arrays and objects could be achived with custom setter and delete methods. For examle if you use proxy object you can use counter for the properties. In the setter if there is not the used property you should increment the counter and in delete the opposite operation.    



  

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Empty array Archos <raul.san@sent.com> - 2012-02-25 10:21 -0800
  Re: Empty array Gregor Kofler <usenet@gregorkofler.com> - 2012-02-25 23:02 +0100
  Re: Empty array Dr J R Stockton <reply1208@merlyn.demon.co.uk> - 2012-02-26 20:50 +0000
    Re: Empty array "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-02-27 14:46 +0000
      Re: Empty array Dr J R Stockton <reply1209@merlyn.demon.co.uk> - 2012-02-28 23:42 +0000
  Re: Empty array Asen Bozhilov <asen.bozhilov@gmail.com> - 2012-02-27 07:30 -0800
    Re: Empty array Dr J R Stockton <reply1209@merlyn.demon.co.uk> - 2012-02-28 22:00 +0000
      Re: Empty array Gene Wirchenko <genew@ocis.net> - 2012-02-28 17:20 -0800
        Re: Empty array Asen Bozhilov <asen.bozhilov@gmail.com> - 2012-02-29 01:23 -0800
          Re: Empty array Gene Wirchenko <genew@ocis.net> - 2012-02-29 14:12 -0800
        Re: Empty array Dr J R Stockton <reply1209@merlyn.demon.co.uk> - 2012-03-01 21:20 +0000

csiph-web