2

Javascript is an incredible language and libraries like jQuery make it almost too easy to use.

What should the original designers of Javascript have included in the language, or what should we be pressuring them into adding to future versions?

Things I'd like to see:-

  • Some kind of compiled version of the language, so we programmers can catch more of our errors earlier, as well as providing a faster solution for browsers to consume.
  • optional strict types (eg, being able to declare a var as a float and keep it that way).

I am no expert on Javascript, so maybe these already exist, but what else should be there? Are there any killer features of other programming languages that you would love to see?

4
  • You might find jslint interesting with respect to your first point. No compilation, but some syntax checking Commented Jul 12, 2009 at 14:55
  • I recommend reading Crockford's book "Javascript: The Good Parts", which also covers the bad parts ;) Commented Jul 12, 2009 at 15:01
  • 1
    Your issues both seem rooted in the expected uneasiness when moving from a statically typed language to a dynamic one. The best remedy for that is unit testing. Unit tests are hugely helpful in dynamic languages. Commented Jul 22, 2009 at 23:55
  • It is more that I know a compiler would trivially reveal all kinds of minor bugs with almost no effort from me, making the most of what computers are really good at. jsLint seems the closest thing to covering that issue though. Commented Jul 23, 2009 at 7:50

7 Answers 7

6

Read Javascript: The Good Parts from the author of JSLint, Douglas Crockford. It's really impressive, and covers the bad parts too.

Sign up to request clarification or add additional context in comments.

1 Comment

Just be careful. Like all experts, Crockford has strong opinions, some of which may be questionable.
6

One thing I've always longed for and ached for is some support for hashing. Specifically, let me track metadata about an object without needing to add an expando property on that object.

Java provides Object.getHashCode() which, by default, uses the underlying memory address; Python provides id(obj) to get the memory address and hash(obj) to be customizable; etc. Javascript provides nothing for either.

For example, I'm writing a Javascript library that tries to unobtrusively and gracefully enhance some objects you give me (e.g. your <li> elements, or even something unrelated to the DOM). Let's say I need to process each object exactly once. So after I've processed each object, I need a way to "mark it" as seen.

Ideally, I could make my own hashtable or set (either way, implemented as a dictionary) to keep track:

var processed = {};

function process(obj) {
    var key = obj.getHashCode();

    if (processed[key]) {
        return;    // already seen
    }

    // process the object...

    processed[key] = true;
}

But since that's not an option, I have to resort to adding a property onto each object:

var SEEN_PROP = "__seen__";

function process(obj) {
    if (obj[SEEN_PROP]) {    // or simply obj.__seen__
        return;    // already seen
    }

    // process the object...

    obj[SEEN_PROP] = true;   // or obj.__seen__ = true
}

But these objects aren't mine, so this makes my script obtrusive. The technique is effectively a hack to work around the fact that I can't get a reliable hash key for any arbitrary object.

Another workaround is to create wrapper objects for everything, but often you need a way to go from the original object to the wrapper object, which requires an expando property on the original object anyway. Plus, that creates a circular reference which causes memory leaks in IE if the original object is a DOM element, so this isn't a safe cross-browser technique.

For developers of Javascript libraries, this is a recurring issue.

3 Comments

My wishlist item would mostly address what you're looking for, too.
True -- I've given your answer an upvote. I wanted to clarify the scenario.
for your specific example of DOM objects there should be a way, since jquery manages to create uniqued IDs for them (used those internally). for the general case of javascript objects this is harder
2

What should the original designers of Javascript have included in the language, or what should we be pressuring them into adding to future versions?

They should have got together and decided together what to implement, rather than competing against each other with slightly different implementations of the language (naming no names), to prevent the immense headache that has ensued for every developer over the past 15 years.

Comments

1

The ability to use arrays/objects as keys without string coercion might've been nice.

Comments

1

Javascript is missing a name that differentiates it from a language it is nothing like.

6 Comments

Well, the syntax was deliberately made to be Java-like, so I wouldn't say "nothing like." Without the Java influence, Eich probably would have made the syntax like Self or Scheme. :-)
scheme-like language, java-like syntax.
Nosredna.... Eich didn't come up with the name "JavaScript"... originally, I believe, it was called LiveScript.
JavaScript is called JavaScript because of a marketing deal between Sun and Netscape; the trademark is still held by Sun, but the Mozilla Foundation holds a license
Eich didn't come up with the name JavaScript. Originally it was called Mocha, then LiveScript. What Eich says: "Whether any existing language could be used, instead of inventing a new one, was also not something I decided. The diktat from upper engineering management was that the language must "look like Java"."
|
1

There are a few little things it could do better.

Choice of + for string concatenation was a mistake. An & would have been better.

It can be frustrating that for( x in list ) iterates over indices, as it makes it difficult to use a literal array. Newer versions have a solution.

Proper scoping would be nice. v1.7 is adding this, but it looks clunky.

The way to do 'private' and 'protected' variables in an object is a little bit obscure and hard to remember as it takes advantage of closures and how they affect scoping. Some syntactic sugar to hide the mechanics of this would be fabulous.

To be honest, many of the problems I routinely trip over are actually DOM quirks, not JavaScript per se. The other big problem, of course, is that recent versions of JavaScript have interesting and useful things, like generators. Unfortunately, most browsers are stuck at 1.5. Apparantly only FireFox is forging ahead.

3 Comments

I'm not sure about '&'. It should be something that's not used for numbers.
D'oh! I overlooked that... I suggested & because that's what most BASICs use if they don't use +. It can't use . like in PHP because that has another meaning. Not sure what else to suggest...
Well I agree the + sucks. Confuses people all the time.
0

File IO is missing.... though some would say it doesn't really need it...

3 Comments

File IO is part of only a few languages like PERL. Mostly it is part of a language's libraries.
the question was not strictly about syntax
A File IO library was, if I recall properly, available in an experimental version of Mozilla browser about 5-6 years ago.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.