Sunday, November 25, 2007

I hate Javascript

You'd think that a guy who used to write some seriously bitching C programs would have no problems working with Javascript, right? For the life of me, though, I just cannot seem to get a strong handle on it. At first, I thought it was the whole divergent "Document Object Models" in Netscape versus IE. You know, the object you needed to "use" was located under document.window.form.thisandthat.value on one platform, and on the other platform it had a completely diffferent path.

Then I learned about the GetElementByID method, and things got better.

Two nights ago, though, when I was trying to come up with a program that would generate a range of valid candidates for rows or columns in Sudoku, I couldn't manage it. The idea was simple enough-- start with an integer set to 123456789, copy/convert it to a string, run a series of tests/checks on it, if it passes then print it in the list and if not then get rid of it, increment the original integer variable and repeat until you reach 987654321.

I couldn't get Javascript to do the whole integer/string conversion bit with the toString() function. It turns out that toString is a method of an object, not just a stand alone function. So you need to do something like this:

var x = 0;
var xString = toString(x); // WRONG!
var xString = x.toString(); // correct!


So Javascript is kind of objected oriented, but not really. It's more like a language that isn't object oriented and encourages plain old procedural programming style, but if you want to do anything really useful with it, you'll need to learn about and use the objects that are included with it.

I think I understand why people get frustrated when they try to learn English. (sigh)