why did they make variables loosely typed in javascript?

Started by
9 comments, last by theOcelot 13 years, 3 months ago
I honestly want to know.
Now I assume whoever came up with the language werent mentally retarded or nuts or sado/masochist's or whatever.
There must be a reason for this major idiot design choice but I just cant think of one.

Im guessing the inventor only designed the language to be used in short scripts eg 5 lines long max + somehow we've now stuck with it as the major client weblanguage by accident.

This may seem a rant but I honestly want to know
Also are there any plans to rectify this design fault with the language?
eg actionscript (flash) they gave the ability to be strong typed in as2.0 or as3.0.
Advertisement
&#106avascript and flash actionscript are both based on something called ECMA script. You can read more about it here if you are curious!

http://en.wikipedia.org/wiki/ECMAScript

There supposedly are pros and cons of using "duck typing", it does seem to be kinda losing favor but if you are interested in duck typing (and dynamic typing) and the reasons for it's existence, check this page out, especially the criticism section!

http://en.wikipedia.org/wiki/Duck_typing
Why do you think it is a design fault with the language?

Many different languages have chosen to use dynamic typing for various different reasons. Popular languages at the moment include Lua and Python. Personally I like how &#106avascript handels various things, including it's use of dynamic typing.
ta Atrix256
http://en.wikipedia.org/wiki/Type_system#Programming_style

looking there, it seems as if the whole thing is a bit of a language war :)

>>Why do you think it is a design fault with the language?<<
is that a serious question?
a myriad reasons,
less bugs, faster program speed, smaller exe size, quicker compile time etc

though from a google it does look like theyre giving the option with &#106avascript2.0 + going strong typed<br><br>http://www.webreference.com/programming/&#106avascript/rg38/2.html<br><br>hopefully this sees the light of day soon, until then I suppose Ive just gotta suffer :(
Quote:Original post by zedz

>>Why do you think it is a design fault with the language?<<
is that a serious question?
a myriad reasons,
less bugs, faster program speed, smaller exe size, quicker compile time etc

It is true that compiler can catch some bugs with static typing, but what about the other ones? &#106avascript isn't even compiled to exe. Is there any benchmarks that dynamic typing causes slower speeds and slower compile times? I'd be really interested to see those.
My favorite part of strongly typed languages is how fast you can analyze and learn other people's code.

Want to know the type of an object? Mouse over it.
Want to know what's in a collection? Look at the type of the collection.

It's very disappointing to read non-trivial &#106avascript/actionscript and have to pretty much go through the same steps to learn the types involved that you do when trying to read ASSEMBLY LANGUAGE.<br><br>Critics will contend that this is being lazy. I contend that programming is hard enough even when the language does everything it possibly can for you, and that willingly using less helpful tools when you have a choice is a kind of masochism.
I also cannot understand what the benefits of dynamic typing are, especially considering the problems it introduces (performance, memory footprint, less readable code, subtle runtime errors). The only thing I can think of is that you may not always need to declare a new variable to hold, for example, the string version of an integer. And that would be laziness, in contrast to the desire, as Nypyren said, for less wasted time on trying to understand other people's code (and possibly even your own I would add).
&#106avascript was done in a few weeks if I remember correctly. Time constraints led to some peculiar design decisions. Then it was the only thing available on the client side for all browsers and the rest is history. I do believe it was designed to be a lightweight limited scripting language, but requirements change as they always do.

That said, dynamic typing is not a design fault.

While I personally tend to hate it for large scale applications, it has its benefits. The two most pronounced are the ease of use and rapid testing. Dynamic language advocates argue that more often than not, programmers using a statically typed language spend more time designing up classes and working around weird hierarchies than actually writing code to do stuff. They also point out how difficult it is to make small iterations in code. Make a 1 line change, run, see results, repeat. It's not uncommon for large applications in statically typed languages to take minutes or hours to build.

So no, I don't imagine anyone's looking into making &#106avascript statically typed. And I don't think there's any chance in hell of getting anything else deployed to pretty much every browser in the world unless it compiles down into &#106avascript.<br><br>And again... this is as devil's advocate. I pretty much prefer explicit, static typing for anything more than 100 lines of code.
Quote:The only thing I can think of is that you may not always need to declare a new variable to hold, for example, the string version of an integer.


yes from my reading this is what some ppl push as a major benifit!
me - say what! :)
though why do u need to decalre a new variable, just use the old one eg print( string(num_variable) )

Im actually typing more cause &#106avascript is sometimes interpretating by var's wrong
y += ParseInt(X)
not to mention the number of this. ive gotta type in front of my variables, eg a typical line in say c++ is like

pos += vel * 0.9;

well in &#106avascript you cant do operator overloading, so its back to the old days of c<br><br>vec3_add( this.pos, vec3_scale_create( this.vel, 0.9 ) );<br><br>Im not sure how exactly it is giving me the ability to get things up + running quicker when I have to type 3x as much.<br>though Im wasting most time by tracking down small typo bugs<br><br>this.variable = 45;<br>this.veriable = 12;<br>console.log( this.variable ) // 45, ok easy to see here by in a long function/class it can sometimes be hard to spot <br><br>@Drathis, see the reasoning why actionscript went from dynamic to static typed data<br>also heres unity's version of &#106avascript<br>http://unity3d.com/support/documentation/ScriptReference/index.Performance_Optimization.html<br><br><!--QUOTE--><BLOCKQUOTE><span class="smallfont">Quote:</span><table border=0 cellpadding=4 cellspacing=0 width="95%"><tr><td class=quote><!--/QUOTE--><!--STARTQUOTE--> 1. Use Static Typing<br><br>When using &#106avascript the most important optimization is to use static typing instead of dynamic typing. Unity uses a technique called type inference to automatically convert &#106avascript constructs to statically typed code without you having to do any work. .... This is &#111;ne of the reasons why Unity's &#106avascript is &#111;n average around 20 times faster than other &#106avascript implementations.<!--QUOTE--></td></tr></table></BLOCKQUOTE><!--/QUOTE--><!--ENDQUOTE-->20x seems a bit too much though<br><br>@Telastyn but from what Ive just read today &#106avascript2.0 will be static typed (or at least give u the option, like they did with actionscript2.0)<br><br>also whats the basic types pass by value, complex type's pass by reference. Whilst usually this is how you'ld want to do it but it would nice to have the choice<br><br>sorry this has turned into a bit of a rant, back to my nearly 10k LOC &#106avascript program :(
Quote:Original post by zedz
Im guessing the inventor only designed the language to be used in short scripts eg 5 lines long max + somehow we've now stuck with it as the major client weblanguage by accident.

heh they're still fixing the typing problems in &#106avascript. Typed arrays were just recently added.<br><br>&#106avascript's future problems have been discussed a lot and it'll feature bloat to death. At least you didn't mention PHP :P<br><br><!--EDIT--><span class=editedby><!--/EDIT-->[Edited by - Sirisian on December 25, 2010 10:49:08 PM]<!--EDIT--></span><!--/EDIT-->

This topic is closed to new replies.

Advertisement