Saturday, 5 January 2013

Javascript - Memory Jogger

Today I 'ave manly been thinking that I need a page if Javascript facts. So I will put it here and see how I get on. I expect it will be a translation of Python to Javascript. (I am considering CoffeScript but more on that later.)
  • I HATE curly brackets and the option to have them or not. Thats what Python has done for me. I LOVE indentation! Bad Javascript/C/C++ etc. 
  • string to int:
  • int to string:
  • dictionary OMG I cant believe the code to sort:
for i in sorted(dict):

is:
var keys = Object.keys(dict);
keys.sort();
for (var i=0; i<keys.length; i++) {
    var key = keys[i];
    var value = dict[key];
    // do something with key and value here...
}
My Rules:

Python has local variables with no declaration but globals need marking as global. This is good because you do not have many global variables ... do you! Of course you have lots of local variable and they all need a var declaration. Hey ho I can get used to this.

I will always use braces even when I don't need them.