Seven deadly and basic Javascript Gotchas
webdigi.co.uk/blog/2009/javascript-gotchas-li...Posted May 21, 2009, 4:09 PM UTC
Posted May 21, 2009, 4:09 PM UTC
"Deadly"? There's no way forgetting the base in a parseInt call is going to OH MY GOD SOMEBODY HELP M...
Well... deadlier than Swine Flu, anyway.
Deadly? Is someone using JavaScript in medical equipment now?
You've seen that telemedicine stuff where doctors in one city control surgery robots in another?
It's only a matter of time before someone comes up w/ a budget version where the robot hosts a web server and the interface is some fancy AJAX stuff.
function doctor() {
if(this.readyState == 4 && this.status == 200) {
if(this.responseXML != null && this.responseXML.getElementById('organ').firstChild.data)
this.responseXML.getElementById('organ').firstChild.data = "babum babum babum";
else
throw new Error("Whoops!");
} else if (this.readyState == 4 && this.status != 200) {
throw new Error("He doesn't need that organ anyway.");
}
}
var patient = new XMLHttpRequest();
patient.onreadystatechange = doctor;
patient.open("GET", "/chest/heart");
patient.send();
Your implementation is not cross-browser compatible. The patient was using Internet Explorer and died on the operating table. Better luck next time, doc!
As a side note: anybody else ever wonder why the "XML" in XMLHttpRequest is all-caps, while the "Http" is not? Seems rather arbitrary, and in a case-sensitive language too.
anybody else ever wonder why the "XML" in XMLHttpRequest is all-caps, while the "Http" is not? Seems rather arbitrary, and in a case-sensitive language too.
No kidding. If they wanted to be consistent it would be xmlHTTPRequest to fit with others like encodeURIComponent etc. On the other hand xmlHTTPRequest looks a little goofy..
This article sucks. All the gotchas are brief code snippets with no underlying explanations.
Several of the examples are in no way specific to javascript but would be gotcha's in any languages. I guess another lame article designed to make the blogger some easy money.
EDIT* Upon further examination the article has misleading information such as telling users js has only 3 primitives, when it has 5 plus the complex Object type. Where are the standards for posting?
The article clears mentions 5 types! Javascript uses three primitive datatypes numbers, strings and boolean. In addition it has two trivial datatypes undefined and null. look here
The link you gave me proves that js has 5 primitives. From your link:
primitive, primitive value A data that is not an object and does not have any methods.
JavaScript has 5 primitive datatypes: string, number, boolean, null, undefined. With the exception of null and undefined, all primitives values have object equivalents which wrap around the primitive values, e.g. a String object wraps around a string primitive. All primitives are immutable.
[deleted]
I don't see how the floating point issue is specific to javascript. In most languages you have to write functions that compare floating point numbers to a degree of accuracy or you can use scaled integers.
in other languages, you have have the choice of other numeric datatypes, in javascript all you have is floating point ;)
It says typeof array returning "object" is bad. It's right though. In Javascript an array is just another object, their is no array data type.
If an array is just another object, and there is no array data type, can you make an object that acts exactly like an array without using an Array constructor or array literal?
Your "array" has to be able to do this:
var array = /* however you want to construct it */;
var len = array.length;
array[len] = 1;
alert( array.length === len+1 ); // must alert true
I think in the latest versions of JavaScript, when you can use __defineGetter__ and __defineSetter__, then yes you can. But in the 1.5 that we all know and love, no.
[deleted]
You should post the other six entries in comments too, then we won't even need to click the link
If you found this useful, you will find Crockford's videos and book highly enlightening. They cover these issues and many more, in much greater depth.
Don't forget the website.
http://www.crockford.com/javascript/
FAIL. How about reading the ECMAScript standard (ECMA-262) before spouting misinformation about implicit semicolons? The standard is precise about when/where semicolons are assumed.
ParseInt function
Parsing ints like "09" as octal is the worst API decision in the history of modern programming languages. It's much worse than PHP's needle/haystack dyslexia combined with every useless factory in the standard Java libraries. I'm going to get down-modded because I speak the truth, and because some of you think horse manure is ice cream.
When is the last time you needed to parse an octal number from a string? Who does that in a web language? Nobody, that's who.
When is the last time you needed to parse a user field where they could be expected to enter "09" for a month or day? Pretty much anyone who's ever done any UI programming. Yes you probably have to sanitize that anyway, but still.
No, you'll get down-modded because of that stupid "I'm going to get down-modded" BS.
Shit like this, combined with lack of any standard libraries, combined with piss-poor debugging options is why Javascript sucks. Yes, I know that the Web 2.0 fairy told you that Javascript is awesome, but trust me, it's not. Go learn several good languages and you'll realize what a shit-pile Javascript is.
Javascript fills a niche that no other language comes close to filling.
Good language or not, it is a necessary for every web app and therefore indispensable .
like, hopefully, objective-j. i need free time!
You're doing it wrong.
Gotcha
[deleted]
[deleted]