views:

308

answers:

6

Just had my mind going today. I spent some time in IE debug mode, browsing the web as usual, and oh boy do I see many errors :) Most of these errors are because some value are of a different type than expected (at least as far as I interpret the error messages).

What are the reasons JavaScript and similar scripting languages aren't strongly typed? Is it just to make the languages "easier" to understand and more accessable, or is the lack of a "compile-time" the real problem?

A: 

My own oppinion: You could parse scripts before executing them. This would catch most type-errors, and mean that the user doesn't have to see a partly-executed-then-terminated scriptresult. Even better, it would be a lot easier to debug the thing, if it had a parser :)

cwap
+2  A: 

It gains flexibility from not being typed. I personally enjoy the weakly typed languages.

So the answer is there'd be benefits and drawbacks.

For people who want a strongly-typed language in the browser, GWT and Script# are available.

Nosredna
A: 

I build a rapid prototype framework for eLearning in ActionScript 2 way back when. My biggest gripe was AS2 was not strongly typed and it causes me many a headache when debugging. I think that strongly typing things makes code easier to read. I think a weakly typed language offers more flexibility.

I lean more towards readability when i have to go back and figure out what the heck is going on in code i wrote 6 months ago.

Jonathan S.
AS2 is strongly typed. Just specify the type after the variable. eg: var vector1:Vector
Marius
+3  A: 

It should definitely have strong typing available. Actionscript 3 is strongly typed, but still has prototype inheritance and a wildcard type if you need dynamic objects.

There are no downsides to having that feature available, and I have to say, for a project of moderate to large size, strong typing prevents A TON of problems. To get the most out of it you need IDE support so it can report errors and provide autocomplete options, but Javascript would be in a whole new world if it had real classes and strong typing.

Sean Clark Hess
+1  A: 

I like the weak typed aspects of most scripting languages for the most part. The only reason that I would want strongly typed, besides for performance, is that it is easier for tools to refactor strongly typed languages than weak.

Rahul Malik
A: 

Would javascript and similar scripting languages benefit from being strongly typed?

Yes, they would, JavaScript 2.0 introduces a type system:

Type System JavaScript 2.0 supports the notion of a type, which can be thought of as a subset of all possible values. There are some built-in types such as Object, Number, and String; each user-defined class (section 6) is also a type.

Also see: http://timkadlec.com/2008/04/an-objective-look-at-javascript-2-0-strong-typing/

In general, support for strong typing provides many interesting opportunities for the compilation and optimization passes.

none