On 06.05.2010 10:06, Biju wrote:
var lastItem =
myObject.anArray[ myObject.anArray.length - 1 ];
myObject.anArray[ myObject.anArray.length - 2 ] = "foo";
a = myObject.anArray; a[a.length - 1];
a[a.length - 2] = "foo";
What is the problem allowing following instead of above?
    a = myObject.anArray; a[-1];
    a[-2] = "foo";


The problem is that currently ES has only concept a "property". And despite the fact that spec defines concept of an "array index", it still "virtual" concept and is only for ability to make some checks or actions (e.g. update "length" property with overloaded [[Put]] method). Still, these properties are only strings. And because of there is no semantic stratification between the dot and the bracket property accessor notations (except the the former is used only when property name is a valid identifier and in advance known), i.e. they are not divided into "keys" and "array indexes" for the bracket notation, and "properties" and "methods" for the dot notation, currently there is no ability to distinguish whether "-2" is a simple (or even inherited) property or means "length - 2" index.

Of course, there could be overloaded [[Get]] semantics for arrays, but this is not in ES5 (who's spec is already published and it's being implemented). The other questions should be clarified then, such as if there is e.g. Object.prototype[-3] = 10; and let a = [1, 2]; then what should return a[-3] if a[-2] returns 1. I.e. should be semantics of the arrays [[Get]] be completely overloaded and do not inherit such property names, or only partly (the last case will cause ambiguity).

But in general, bracket slicing (used widely in some languages) is more convenient than explicit "slice" method which does the same. But notice, that in contrast with ES in those languages there is stratification in semantics of the dot and bracket notations.

In contrast, proposed "last" method seems also ambiguous in case of setting and looks a bit ugly. That taking into account that there are method names with "get" semantics. So the setter "last(-index, value)" looks odd. So I don't think this proposal is very required. For convenience as maximum you can implement only a getter in the Array.prototype for just a.last.

It would be incompatible, since a[-1] means the same thing for arrays
as for objects: the property named "-1".
And I dont think any body depending on such code now,
ie expecting "-1" as property name in Array

I have had once upon a time ;)

Dmitry.
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to