PDA

View Full Version : Ext.isArray wrong implementation



mabello
13 Jun 2008, 2:23 AM
I'm not too excited about how Ext.isArray is implemented (also Ext.isDate actually...), I'm a little bit worried.

For example it does not work with something like this:



Ext.isArray(null);//return null!?!?!?!?
Ext.isArray(t);//t is undefined, return exception!?!?!?


I would like to return in both case false...not clean at all in my opinion.
So check first of all if the passed object is undefined or null, then if it is an object and than you can do something like:


obj.constructor == Array

to check if it is an array.
That is clear in my opinion. What do yout think?
Thanks

Animal
13 Jun 2008, 3:33 AM
Fixed in SVN.

mabello
13 Jun 2008, 3:43 AM
Well done, I have my patch, but this is really a basic function used in the core of Ext, so it's really important that it works fine...
Thanks for the reply

mabello
13 Jun 2008, 3:44 AM
And by the way another important thing:
I've seen in a lot preconditions in the core, Ext uses !obj ...it is really wrong as well!!
if obj is undefined, you have an exception...really scary!
EDIT:
I always use to check a function like isNullOrUndefined:


Ext.ux.Utility = {

isNullOrUndefined: function(obj) {
return (typeof obj == 'undefined' || obj == null );
}
}

That's really better

Animal
13 Jun 2008, 3:48 AM
"!undefined" evaluates to true.

mabello
13 Jun 2008, 3:50 AM
And it is bad in my opinion...if obj is a boolean and is false, !obj return true as well, as it is undefined, not good at all for me! So !obj is too ambiguous
And I don't think you are right by the way: if I edit in firebug this:

if(!zzzzz)
alert("");

I have actually an exception, zzzzz is undefined

evant
13 Jun 2008, 4:00 AM
It's generally always a named parameter:



var o = {};
myMethod(o);

function myMethod(obj)
{
if (obj && obj.prop)
;//foo
}

tryanDLS
13 Jun 2008, 9:39 AM
And I don't think you are right by the way: if I edit in firebug this:

if(!zzzzz)
alert("");

I have actually an exception, zzzzz is undefined
There's a difference between referencing a var which has not been declared (what you're doing above) and referencing a var that has been declared but doesn't a value. If you add a var zzzzz stmt in FB before entering that line, you will not throw an exception.

Since the Ext code is referencing a function arg, the var is declared whether or not the caller passed anything.

mabello
13 Jun 2008, 11:45 AM
Fair enough. But the point remain: if I see in the code !obj I can not help thinking that obj is a boolean and you want to verify if it is true or false.


function c(b){
alert(!b);
}

c();
c(false);
//Same results


I do not like !obj to test if an object is defined or not, as you can check from my example it could be confusing, that's my personal opinion, but I would say it is a question of taste :)
Thanks

danh2000
13 Jun 2008, 4:16 PM
So check first of all if the passed object is undefined or null, then if it is an object and than you can do something like:


obj.constructor == Array

to check if it is an array.


That's not a reliable method - Safari on the PC returns "function" for the constructor.

Better to test for the existence of one of the array methods.

Animal
13 Jun 2008, 10:50 PM
That's what the fixed version in SVN does... take a look.