by MrEnigma · 14y ago · 0 comments
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.
[] != []
[] == ![]
{} + [] is 0
[] + {} is "[object Object]"
{} + {} is NaNI'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.
Using jQuery for this does not mean that you like or dislike the language. That was the point I was trying to refute.
Animations is of course also not the only problem you can solve by using jQuery as a library every now and then.
----
For what it's worth, I don't think jQuery qualifies as a framework in the definition on Wikipedia ( http://en.wikipedia.org/wiki/Software_framework ), but this is besides the point.
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?
You said: "the fact that very few people code pure javascript (...) goes to show how much people like it!"
I argue that using jQuery does not change the "pure JavaScript"-ness of any code, just like using Rails does not change the "pure Ruby"-ness of any code, just like using libpng does not change the "pure C"-ness of any code. In every case, it's all in one language anyway -- therefore it is pure in that language.
This comment is not intended to be an argument either way. I am just trying to clarify exactly which point of view I have been trying to argue :)
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.
I think another interesting thing to note is that many languages that do make a distinction (at the type level) between integer types and floating point types (that is, ones like IEEE 754 floats with defined NaN and +/- Infinity behavior) do allow 1f / 0f [1], but raise an error on 1 / 0. Avoiding automatic type coercions (either between ints and floats or between floats and strings) would make it more clear what actually happens in that case.
[1]: Except, apparently, Python! Who knew: http://bytes.com/topic/python/answers/769104-turn-off-zerodi... ?
I weakly disagree on #2. There are cases where +Inf is valuable, but I've never seen it in the browser. That doesn't mean it doesn't exist in the browser, just that I think it is an error far more than it is expected.
I think #3 would need to exist because 1 and 2 are wrong.
I totally agree with #5. That little simplification is the source of endless subtle bugs.
And #6 is an interesting one. I think it is related to #5, though. A language should first and foremost be consistent. If it automatically coerces in some conditions, it probably should in all[1].
1. Said while it is late and I haven't really thought through all the potential ramifications.
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"
So not a bug, though I agree the string search is particularly silly. It can't be a bug if it does what the specified algorithm says it does. It would be a bug if it gave a different result.