Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Friday, August 8, 2008

Why I hate Javascript

Basically because I don't know it... and I don't want to get aquainted with it!!! Maybe it is because I'm so accustomed to the Java language, maybe because I'm dumb, maybe for a combination of these and several other factors. Anyway, I'll post some tips and some scripts that, albeit very easy, drove me crazy simply because I couldn't find their right syntax.

If you need to create an object in Java you must have a particular class and call its constructor using the "new" keyword (Yes, we can also use a factory method, a creation method, the newInstance method of the class, or other stuff, let's not be pedantic):
public class MyObject {
private String attr1;
private int attr2;
public MyObject(String attr1, int attr2) {
this.attr1 = attr1;
this.attr2 = attr2;
}
public String toString() {
return attr1 + " " + attr2;
}
}

...
MyObject obj = new MyObject(myString, myInt);
System.out.println(obj.toString());
...
In Javascript you simply create an object and add properties and methods to it:
personObj = new Object();
personObj.firstname = "John";
personObj.lastname = "Doe";
personObj.age = 50;
personObj.eyecolor = "blue";
But the interesting thing comes with methods. If you want to invoke a method in Java it must exist as public (yes, also default, or protected for subclasses, you could use reflection, I'm simplyfing for simplicity's sake!) in a class assignable to the object. In Javascript you simply add it:
addressObj.myOwnMethod = myOwnMethod;
If you want to call that method you use the syntax
addressObj.myOwnMethod();
BTW, all semicolons at the end of the lines are optional, just to make things weirder. And what happens when you call a method? you are really calling a function defined elsewhere, something like
function myOwnMethod() {
...
}
If your function has parameters, its declaration would be something like
function myOwnMethod(par1, par2) {
...
}
and if you want to call it you use the syntax
myObject.myOwnMethod(par1, par2)
What if you want to hide the parameters of the function you call, e.g. you ask a shipment for its cost, which is calculated on the basis of some attributes of the shipment itself? In Java you simply ask it to the shipment instance:
double shipmentCost = myShipment.getCost()
In Javascript you do the same, but it works in a slightly different way (maybe that's not the only one, but it is the only one I managed to get working). You have a method in the object without parameters, as one would expect, you have a function without parameters, as one would NOT expect, that gets called from the object when you invoke its method and it uses the "this" keyword... The "this" in a function!!! and it does not refer to the function but to the object that invoked it! how counterintuitive!
...
// inside the definition of an object (if using a template=
// or simply after the instantiation of an object
myShipment.getShippingCost = getShippingCost
...

// outside the definition of the object
function getShippingCost() {
var cost = 0;
if (this.DestinationAddress == "Boston") {
cost = (this.ShippingType == "STD") ? 100 : 200;
} else if (this.DestinationAddress == "Seoul") {
cost = (this.ShippingType == "STD") ? 500 : 1000;
}
return cost;
}

...
// somewhere else...
myShipment.getShippingCost();
It just gives me a headache... it's such a juggling act trying to learn Ajax technologies while keeping your mental health...

Thursday, August 7, 2008

W3Schools Javascript Quiz

Just completed the W3Schools Javascript Quiz and got 17 correct answers out of 20, which makes 85%, in 3:00 minutes. The computer tells me I can be proud of myself, but the acid test will come when I need to use it in a production environment...

A new use for Google Maps

Surfing around the web looking for Ajax applications I decided to try Google Maps paying attention to the technical stuff beyond it (want to see the code? try JSView, a Firefox plugin - BTW, I hope that the Google guys obfuscated it, because if that's the way they actually code... urgh! I can't understand anything!).

Anyway, I typed my address, hit the button and bang, the map smoothly appeared under my eyes: wow, awesome! And it is so easy to move around, and the responses you get are so fast! On the left there are additional informations, like pictures of places situated in the vicinity of the point identified using your search string. Another additional information is represented by personal maps, which is a nice feature that enables you to pinpoint interesting places. I saw such a map and decided to check it and... I could not believe it... I'll report an abstract of the description so you can get an idea yourself:

XXX is a 22 years old, pretty, blonde, long wavy hair, likeable, a very easygoing girl. She stands near YYY, under the railway bridge coming from ZZZ, pity she's a little plump.


I entered puzzled mode and went like "Ow, there must be some mistake... let me check this guy's maps..." and in the beginning everything looked fine, some restaurants and stuff, but going on you find a (probably) incomplete list of all the harlots he pays visits (among other things) to... and for each one he provides not only the location but also a brief description.

<ironic mode>Now, that's a service! (pun not intended) if you're looking for new friendships, including foreign cultures representatives, you have another ace in the hole (ehm... pun not intended)</ironic mode>