"VK" <schools_ring@yahoo.com> writes:
[color=blue]
> Well, I'm affraid my vision is the only one that stops us from
> declaring native array methods "broken".
> My point always was that we need to see the difference between the
> hardware reality and the human abstractions to describe it.[/color]
I can agree on that. It is more the words used to describe the
abstraction that is my problem.
[color=blue]
> I just see another simplification of JavaScript, that forces us
> sometimes to name and notate very different things in the same way. And
> after that to have discussions "What kind of Joe is it really?"
> JavaScript has only undefined and null. So by describing the situation
> "within array" / "outside of array" we can only talk of "undefined kind
> 1" and "undefined kind 2", and it's really confusing (but still needs
> to be understood).[/color]
My problem is that you use the *values* to distinguish "inside" and
"outside" the array. The values are the same, and (at a lower level)
for the same reason.
What distinguishes being inside and outside the array, is the indices,
not the values.
So, in other words:
When using the notation arr[index] , where "arr" is an Array object,
one of the following two scenarios holds:
If your index is a non-negative integer less than the length of the
array (as given by the "length" property), then you are accessing
an array element. It will be undefined if no value have been stored
at that index.
If your index is either not a non-negative integer, or not less than
the arrays length, then you are not accessing an array element, but
merely a property of the object.
Not perfect, but moves the distinction from undefined 1 vs undefined 2
to the indices used to access them.
[color=blue]
> If we move on on say VBasic.Net, we have the standard triad Nothing /
> Empty / Null.[/color]
I'm not sure what makes it so standard, since I have never heard about
it before (but then again, I never did VB).
[color=blue]
> So:
> 1) something set *by myself* to be nothing is Null.
> 2) something appeared "against its will" in an enclosing structure (but
> never directly set yet) is Empty.
> 3) something totally out of the scope of my program is Nothing.[/color]
I prefer throwing exceptions when people go outside of the scope of
the program, and initializing everything, so I would probably only
see Null. (Does it show that I'm usually doing Java? :)
[color=blue]
> So if
> arrayObject=new Array(); arrayObject[100]=null;
> then arrayObject[100] is Null, arrayObject[99] is Empty, and
> arrayObject[1000] is Nothing. It gets much easier, is it? :-)[/color]
It makes some kind of sense, but I would presonally prefer to remove
"null" from Javascript rather than have one more value that means
really means "no value".
[color=blue]
> I hever was a Macintosh user, sorry. I guess the current platform is
> OX?[/color]
OS-X is not a Javascript platform, so it is probably Safari. It still
has a few quirks in its Javascript engine, but I bet they'll be sorted
out soon enough.
[color=blue][color=green]
>> There is no guarantee that implementations have efficient property
>> look-up. It seems like IE have linear time look-up of properties.[/color]
>
> I don't think so, because object properties (if you list them unsorted)
> corresponde to the model "memory baskets" strusture.[/color]
If I create an object and add the properties "x0" .. "x99", and then
read them out using for(var k in arr) {...} then they come out in the
same order as they were added, no matter if I add them forwards or
backwards. That shows no "memory basket" behavior.
Also, try this code:
---
var N = 10000; // try varying N.
var arr = {};
for (var i = N-1; i >= 0; i--) {
arr["x"+i] = true;
}
var res = [];
var t0 = new Date();
for (var i = 0; i < N; i++) {
var t = arr["x"+i];
}
var t1 = new Date();
alert((t1-t0)/N); // note - divides by N!
---
If IE was using a hash structure, then the result should be approx.
the same no matter what N is (limited by the granularity of the timer).
Instead it seems that doubling N gives an increase of little less than
a doubling ... i.e., accessing properties is not constant time, more
like linear.
[color=blue]
> Their interpreter can have some bug in it though. Any reliable time
> mesurements around here? (and please on real hash, not on a build-in
> object's properties).[/color]
I hope that above qualifies :)
[color=blue]
>[color=green]
>> What is an "out of papers" language?[/color]
>
> "out of papers" - first think through, then do (like C)
> "out of life" - first do, then look what happens (like LiveScript or
> Perl).
>
> Any better terms?[/color]
"designed"? :)
[color=blue][color=green]
>> I disagree. Using numeric indices is not stable against changes
>> to the page. If you insert a form before the one you refer to here,
>> you also need to change the index. Instead use:
>> document.forms['formId'].elements['elementName']
>> widow.frames['frameName'][/color][/color]
....[color=blue]
> See the cases I linked to the article. The isssue is to be sure that
> you always get something what you asked for, not a substitution, even
> if it is also usable sometimes.[/color]
You always get what you ask for. Computers are not that inventive.
The big question is whether you know what you ask for.
If you have more than one form control with the same control name,
then accessing by name is obviously not unambiguous. You then get
a collection of the matches. But you *should* know whether the name
is unique, and if you don't, it's better that the code fails as
soon as possible.
I still recommend accessing by name, not index. Indices are too
fragile when the page changes.
[color=blue]
> For the same reason we're talking about "undefined kind 1" and
> "undefined kind 2". Different notation eliminates the question of what
> are we doing or trying to reach.[/color]
It all comes back to arrays not *really* being part of the language.
There is just an *object* with array-like behavior (which pretty
much boils down to the magic "length" property ... ignore "length",
and there is no difference between Arrays and Objects)
[color=blue]
> The super goal is to pull out array and hash as separate and very
> different *programming entity* from your "Great Mother"-like
> HTMLCollection that supposes to include all and explains everything.
> Because it doesn't, at least not anymore.[/color]
I would rather define Array and Associative Array ("hash" smells too
much like a specific implementation) separatly, then show how each
can be approximated by a construct in Javascript (Array and Object).
In both cases, it is not a clean implementation, the underlying
object shows through the cracks. An object is never completely
free of properties (inheriting some from Object.prototype), and
an array is really just an object with a magic "legth".
[color=blue]
> The mini goal to address numerous issues that should be included into
> FAQ's such as:
>
> 1) arrayObject = new Array(); arrayObject['foo'] = 'bar';
> arrayObject.length == 0;[/color]
I'm not sure it qualifies as a *F*AQ, but it's worth repeating: Don't
use arrays unless you mean it.
/L
--
Lasse Reichstein Nielsen -
lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'