55 points · by onemach · 14y ago · 66 comments
So yes, this is nothing to do with JS being a dynamic language (which main feature is not a variable/slot but a value having a type), but it's something to do with it being a weakly typed language (which main feature is an implicit and generally unpredictable type conversion). Of course many popular dynamic languages are still strongly typed in this sense.
Javascript it's seen as an amusing conundrum.
Also, the fact that very few people code pure javascript, instead choosing frameworks like jquery, goes to show how much people like it!
However, I'd say the use of libraries and frameworks help javascript programmers, for sure, but that doesn't make the language any less of a mess (and in some cases, like jQuery setting "this" to whatever the hell it wants, makes it more of a mess). I can't speak for php, because I've used it far less, but I suspect the same holds true there, too.
I'd say most languages are coded in frameworks and not just the "pure" language.
jQuery does nothing to alter the language, so people using it are still coding pure JavaScript. jQuery itself is implemented in pure JavaScript.
----
There does exist variants of JavaScript, such as Paper.js, Protovis and maybe even CoffeScript, but these are not what the majority of JavaScript users use.
JavaScript in the browser has access to the standard JavaScript and DOM libraries. I really don't think that not using any third-party libraries in any language means "you're doing it wrong". Pure JavaScript/DOM can go a very long way (especially in these days of more standard-compliant browsers), just like pure Objective-C & Cocoa or pure C# & .NET.
Finally, while obviously jQuery does not alter the language, it really changed how people approach problems in JavaScript. Code that uses jQuery looks nothing like code that doesn't use it.
I don't agree to "does nothing to alter the language" though. It changes the way you manipulate DOM across your codebase. When one says 'library', I assume it's something you make calls to to achieve certain functionality. Like an image library or oauth library.
Also, that jquery is implemented in javascript is not relevant in my opinion. Rails is implemented in ruby. Does that make it not-a-framework?
print intval(NaN, 19);
Result in PHP 5.2.17 is: 0
Clearly, that is correct.PHP does the following:
intval('42', 8); // => 34
intval(42, 8); // => 42
Now try: print intval(strval("19.99"*100));
print "<br>";
print intval("19.99"*100);
This returns: 1999
1998Unrelated, it seems like intval(1/0, 19) in PHP also returns 0 because 1) intval returns 0 if given an empty string (JavaScript's parseInt returns NaN if given an empty string) and 2) 1/0 seems to return something hilarious... it converts to "", but gettype() on it returns "boolean"... I sadly don't know PHP well enough to understand what it is ;P.
PS : I know it had been designed in 11 days and as such it is an achievement, but as the most used programming language it is quite awful.
The fact that we are looking at parseInt as a potential problem here doesn't even make sense, given that the way most developers at this point actually expect functions like parseInt to work is to stop at the first non-number, and the developer in this case specifically went out of their way to choose a radix where I is a number.
Things that could have happened instead:
1) parseInt could fail if it is passed a string that contains anything that is not a digit (this would surprise many developers: again, this is highly common behavior)
2) 1/0 could throw an exception (I would argue this isn't even useful: +Inf is a valuable result)
3) +Inf could return something other than Infinity if converted to a string (maybe the infinity symbol in unicode?)
4) +Inf could refuse to be converted to a string (this is awkward, given that any other number can be converted to a string)
5) numbers could always refuse to be automatically converted to strings (I actually agree with this, but I feel like I'm in the minority: 'a' + 4 + 'b' would therefore also hopefully be illegal; this is an argument for a separate string concatenation operator)
6) parseInt could specifically refuse to automatically convert its argument to a string (this is probably the most reasonable thing that could have been different; however, tons of languages, including ones considered to have amazing type systems like Scala, support implicit conversion and don't even offer this kind of override flexibity)
Which are you claiming is the bad design? Maybe there's something I'm missing? I mean, if this was a situation where 4 + '1a' yielded the result '5' I'd be sufficiently angry as to claim that the people who designed this language were incompetent or dangerously negligant (PHP probably does this... MySQL almost certainly does ;P j/k, btw, only semi-serious), but this behavior seems somewhat reasonable.
Lack of type safety in a dynamic language is one thing, but awful type coercion is a different beast altogether.
Javascript allows numbers to be a number, NaN or positive/negative infinity. It is well specified within the spec that "Division of a nonzero finite value by a zero results in a signed infinity. The sign is determined by the rule already stated above." [1]
Before you get your knickers in a twist, this is compliant with IEEE 754; although the IEEE define it as an exception.
1. ECMA-262, pg 74, "11.5.2 Applying the / Operator"
Javascript is no better than PHP. It is there - like Php in its time- because we have no other choice.
We're supposed to be professionals and one of our main tool is a joke.
If you're going to complain about language choices, don't force everybody on Python!
I can't comment on Ruby because I have never had to use it.
What would be perfect is Scheme in the browser. And, apparently, we almost had it except marketing or management forced Eich to make it look like Java resulting in JavaScript.
Or from forking Chrome, since it's open source, and then offering your patch back to the central repo. Not sure if Firefox is open source, but could use that instead, too, if it is.
That's why I added the part about forking and contributing back. If it becomes part of the standard offering, then you don't have to worry about market share as much.
And especially if you get a portable implementation then the IE and Chrome teams might decide to adapt it in, since you've lowered the engineering cost.
> But that's besides the point. The behaviour is actually well defined
There's a good reason to not use Javascript, if there's an option to use another language in the browser, that has nothing to do with JS's quality itself: context switching. Node.js is popular due in no small part to the fact that you have to use JS anyways. The purpose of GWT, as far as I'm aware, is to make it so you have Java as an option in the client.
Chrome is a closed source browser based on the open source Chromium, and it includes some features that the open version doesn't, like embedded PDF reader or MP3 support.
Incidentally, PHP doesn't have the concept of positive or negative Infinity, just NaN. However, when I tried:
print inval(1/0);
I got an exception. But point taken :-)Edit: I might not be correct about positive or negative infinity in PHP... apologies if I have at all mislead anyone!
Also: got a proxy error, so this got reposted, then I deleted one of the posts, but the other post got marked deleted. So third post about this!