Sign in to follow this  

why did they make variables loosely typed in javascript?

This topic is 2549 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
javascript 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

Share this post


Link to post
Share on other sites
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 javascript handels various things, including it's use of dynamic typing.

Share this post


Link to post
Share on other sites
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 javascript2.0 + going strong typed

http://www.webreference.com/programming/javascript/rg38/2.html

hopefully this sees the light of day soon, until then I suppose Ive just gotta suffer :(

Share this post


Link to post
Share on other sites
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? javascript 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.

Share this post


Link to post
Share on other sites
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 javascript/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.

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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
javascript 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 javascript 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 javascript.

And again... this is as devil's advocate. I pretty much prefer explicit, static typing for anything more than 100 lines of code.

Share this post


Link to post
Share on other sites
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 javascript 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 javascript you cant do operator overloading, so its back to the old days of c

vec3_add( this.pos, vec3_scale_create( this.vel, 0.9 ) );

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.
though Im wasting most time by tracking down small typo bugs

this.variable = 45;
this.veriable = 12;
console.log( this.variable ) // 45, ok easy to see here by in a long function/class it can sometimes be hard to spot

@Drathis, see the reasoning why actionscript went from dynamic to static typed data
also heres unity's version of javascript
http://unity3d.com/support/documentation/ScriptReference/index.Performance_Optimization.html

Quote:
1. Use Static Typing

When using javascript the most important optimization is to use static typing instead of dynamic typing. Unity uses a technique called type inference to automatically convert javascript constructs to statically typed code without you having to do any work. .... This is one of the reasons why Unity's javascript is on average around 20 times faster than other javascript implementations.
20x seems a bit too much though

@Telastyn but from what Ive just read today javascript2.0 will be static typed (or at least give u the option, like they did with actionscript2.0)

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

sorry this has turned into a bit of a rant, back to my nearly 10k LOC javascript program :(

Share this post


Link to post
Share on other sites
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 javascript. Typed arrays were just recently added.

javascript's future problems have been discussed a lot and it'll feature bloat to death. At least you didn't mention PHP :P

[Edited by - Sirisian on December 25, 2010 10:49:08 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by TelastynDynamic 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.

This.

Share this post


Link to post
Share on other sites

This topic is 2549 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this