JS: The Good, the Bad and the Ugly 12

An article comparing JScript and VBS got me some ideas about what I think should be added to JS 1.6.

Date

I like the Date object in JS but it needs a lot of improvements. These following things are really missing:

  • Localized weekday name
  • Localized month name
  • Week start day
  • Week number (localized because it is not the same all over the world
  • parse is totally useless because it only accepts US date format. It should allow a localized date string and take second argument containing the pattern.
  • There should be a way to get the pattern, much like how SimpleDateFormat works in Java.

String

We definately need a format method that works much like printf in C and the % operator in Python. This one should work with the date time format mentioned above.

There is also a need for a StringBuilder class since concatenation is too slow to do on imutable String objects. This is easy to implement in JS but letting the host environment implement this could improve performance quite a bit.

Array

See Array Extras.

These methods should also work on hashes (Object) and other collection like objects like the DOM NodeList.

Object

  • There is often a need for a toHashCode() method that returns a unique id of the object
  • Getters and setters (Mozilla has these in JS1.5) but they need to be widely supported to allow real world usage
  • Marking fields as enumarable or not. ActionScript has had this for a long time.
  • A way to override the [] operator. I mean right now there is no good way to extend Array or Object to make it behave like a collection without adding a method.

Other

  • Constants like in JS1.5
  • for each loops that loops over the elements in an Array and the fields in an Object. (E4X really blew it here and broke the for in loop semantics.)
  • Depreciate with since it is really bad in dynamic lanugages
  • Add tuples support
  • Fix the constructor property so that it points at the constructor used to create the object.
  • Allow sub classing of built in objects like Array, Function and Date as well as DOM objects. Just imagine being able to create a class that extends Element
  • yield/generator support like in Boo or Python.

12 thoughts on “JS: The Good, the Bad and the Ugly

  1. Tom Trenka Jul 14,2005 20:08

    Byte Arrays! You forgot byte arrays!

    Oh, and full on operator overloading would just f*cking rock :)

  2. Erik Arvidsson Jul 14,2005 20:25

    Yes. Totally. Using arrays with Number/Boolean is very slow and using Numbers with bitwise operator is limited to 32 bits. I guess the best thing to do today is to use a combination of the 2?

    Yeah, why limit the operator overloading to []? I would really love to be able to overload ‘+’ so that we can implement event listeners like in C#.

    I also like the unit things from JS2 but the line has to be drawn somewhere.

  3. M. Schopman Jul 15,2005 11:00

    I would love to see “metadata” features so we can automatically generate documentation. The scripts become larger and more complex, documentation is necessary. Something like C# has, with /// summary

    Not to mention a simple super scope. Absolutely necessary :)

    In overall beter focus on providing editors with more information and features for Javascript coding.

  4. Erik Arvidsson Jul 15,2005 11:08

    Metadata for documentation is not really needed in my opinion. It works well with jsdoc and similar tools already. JS2 has support for attributes (like JScript.NET)

    Super scope? Necessary? not really, but convenient. You can invoke super and super methods using call and apply on the function objects in question.

    I agree that editors totally suck today but I don’t think it is a problem that the syntax and semantics of the language should care about.

  5. Dean Edwards Jul 15,2005 20:51

    If we just got getters and setters I’d be happy.

  6. Jeremy Jul 16,2005 06:50

    I’m with Dean. While the list is nice, I’d be jumping up and down with joy if getters and setters were implemented. What a wonderful world life would be.

  7. Erik Arvidsson Jul 16,2005 10:34

    I agree with getters and setters being the most important thing here. I’m using ul elements because the list is unordered ;-)

  8. Nicholas Aug 1,2005 19:59

    Oh man, date weeks drive me nuts. I had to come up with an algorithm for that at my last job, and it was painful. So many different standards, so little time.

    In regards to stringbuilder: Firefox actually fixed the problem with string concatenation. I had created a StringBuffer object for my book before Firefox 1.0 was released, and now I find that Firefox’s native string concatenation is actually faster than my StringBuffer (IE’s is still significantly slower). While having a native StringBuffer would be nice, it would also be nice if JavaScript worked the same way as Java in that behinds the scenes it used a string buffer so we didn’t have to worry about it.

    Marking fields as enumerable is a big one for me as well. I’m sick of overriding toString() only to have it show up in a for..in loop.

  9. Erik Arvidsson Aug 1,2005 20:15

    I guess += can be optimized for strings but it makes other things a lot harder to implement (in the interpreter that is). But as long as it works I’m happy. I didn’t know Java optimized this. I know JS.NET gives a warning when you use += on Strings.

  10. Garrett Smith Dec 16,2005 10:28

    Those would be very good things to have, Erik. Very practical and wouldn’t be that hard to add to the language.

    * String could also have instance methods:
    trim, startsWith and endsWith.

    * string.match and RegExp.exec should both be fixed to always return and array, and never null, thus eliminating the need to make an extra check for null. So you could do this:

    var matches = “foo”.match(/o/);
    for(i = 0; i

  11. Erik Arvidsson Dec 16,2005 11:21

    Garrett: You ended up sending your comment to the JS1.6 thread

  12. Pingback: nrpneinzc

Comments are closed.