as i said, this is just a thought. The best use case i can think of is
something like this:
function doSomething(obj){
if ($count(obj)){
//if the object is empty or undefined or anything falsy
}else{
//if the object is useful
}
}
2 places where i can see thins as useful:
1. it's easier than to remember what is the right method for your
specific variable (does it have a length property? what is the method to get
it's length if not?).
2. the second is when i want to know if a variable is empty. in PHP,
empty arrays are falsy. In JS, empty arrays and objects are truthy, and
there are many case where you would like to easily check if that variable is
empty. In a way, this use case is more like $isEmpty than $count
the rest of the use cases ('regexp','function' etc) are just so that there
will be a standardized way to handle them if passed.
-----------
אריה גלזר
052-5348-561
5561
2010/2/11 Fábio M. Costa <[email protected]>
> I think that your function looks fine, ive just changed some little stuff:
>
> http://www.jsfiddle.net/tvYQT/3/
>
> i think you need to use getLength() (not sure) on the hash for example.
>
> But if you could explain us why you need this kind of functionality, and
> whats the problem your having, maybe we could come with a better solution.
>
> Cheers,
>
> --
> Fábio Miranda Costa
> Solucione Sistemas
> Engenheiro de interfaces
>
>
> On Thu, Feb 11, 2010 at 4:29 PM, אריה גלזר <[email protected]> wrote:
>
>> i've been playing around with implementing a count method in JS, that will
>> return the length of a given variable, no matter what it's type is.
>> this is mainly useful for switching between array/object/string, but also
>> might be interesting to see it work with elements.
>> obviously, the main problem is how to handle types that have no distinct
>> length, like functions, regexp etc.
>>
>> I've come up with this http://www.jsfiddle.net/tvYQT/1 as a proof of
>> concept.
>> What do you think? would you find this useful? how would you handle
>> non-lengthy types?
>> -----------
>> אריה גלזר
>> 052-5348-561
>> 5561
>>
>
>