VK said:
Hum... indeed. That's kind of a silly behavior I did not
notice before - as I never used delete on array elements.
And you only needed to be told why your original - isArray - function
was inadequate three times before you tried it for yourself.
IMO delete should work the same way as delete
someObject.someProperty - not keeping someProperty
with undefined value but _delete_ it as it never existed.
That is exactly what - delete - does do. I don't know what exactly
passes as testing with you but it doesn't appear to leave yo0u any
better informed.
Well, it's easy to fix:
function isArray(obj) {
var ret = false;
var len = 0;
if (obj) {
len = parseInt(obj.length, 10);
if (len == len) {
obj[len] = 'probe';
ret = (len < obj.length);
obj.length = len;
}
}
return ret;
}
You really don't understand what the code you write does, and even when
your faults are pointed out you just don't know how to correct them. You
have change code that modifies the - length - property of all the arrays
that are exposed to it into code that adds a new (or changes an
existing) property to (on) every non-array object it encounters that has
a numeric - length - property (which, at minimum, includes all String
and Function objects, plus many custom objects). A test function really
should not permanently modify the objects that it tests.
What browser did you test it on?
Firefox, Opera and IE (though it is pure ECMAScript so would be expected
to work with all anyway). Why didn't you test it? Or why couldn't you
manage to perform such a simple test effectively?
Of course it fails (false).
When the - isArray - function is the function that you posted and
declared "absolutely robust", that was:-
| function isArray(obj) {
| var ret = false;
| if ((obj) && (!(isNaN(parseInt(obj.length))))) {
| var len = parseInt(obj.length, 10);
| if (len == obj.length) {
| obj[len] = 'probe';
| ret = (len < obj.length);
| delete obj[len];
| }
| }
| return ret;
| }
- the result is exactly as I described; the alert is 'isArray(obj) =
true'. You could verify that by trying it yourself, as anyone else can.
As I said, there is no technical way to implement
compound property within ECMAScript 3rd ed.
As your whole "compound property" thing was a fiction with no reality
outside your own head anything you say on the subject is irrelevant.
It's not a teasing for an intellectual challenge,
just a constatation of a fact.
It is a fiction.
Respectively this isArray
"This isArray"? Not the previous one?
So there can be no object for which - isArray(obj) - (where - isArray -
is your new version rather than the previous version that reported -
true - for the object I already posted) returns true?
One of the consequences of your basing your conception of javascript on
your ludicrous fictions is that you end up making statements that are
easily demonstrated as false. This object:-
function AnObject(){
;
}
AnObject.prototype = {
length:{
valueOf:function(){
return 1;
},
toString:function(){
return '0';
}
}
};
var obj = new AnObject();
alert(('isArray(obj) = '+isArray(obj)));//alerts isArray(obj) = true
- returns - true - from your newest "absolutely robust" array test, even
though it is clearly not an array. It does so for precisely the same
reason as my previous object returned true from your previous -
isArray - test. And if you had bothered to test my previous object with
your previous test you may have seen the obvious truth that would have
suggested that you not label this test as any more "absolutely robust"
as your last easily fooled test.
yet absolutely not needed with instanceof alive.
If you had understood my criticism of your use of - instanceof - in this
context you would be able to see why that would not help anyway.
Richard.