I've been doing a lot more reading on Java and it's very interesting in how it compares to Javascript. Here are my programming observations of the day. Maybe some of you will find it useful and hopefully some of you will share your own. You learn a lot that you may have missed out on by writing about what you learned initially.
- In Java, there is not really any sense of 'global variables' since everything has to be put in a class. In Javascript, global variables are everywhere unless you know how to use functional scope to circumvent that.
- In Java, once you get past all the type declarations and 'access modifiers', I'm finding it a bit more readable than some of the Javascript since Javascript is so free that you have people writing all kind of code. When you have functions and objects being returned in functions and method chaining, it starts to get hard to follow some of the code.
- Class-based inheritance in Java is more easily understood and straight forward. Protypal Inheritance in Javascript is more powerful, since it can actually be used to emulate class-based inheritance.
- The 'this' keyword in Javascript is pure madness. The context of 'this' can change depending on what function is calling it. If some code is being called by the object's method, then this refers to the object that called it. However, you can use methods named 'apply' and 'call' which allow you to borrow methods from other objects and apply them to other objects. This changes the context of 'this'.
- Java has better development tools, but I love how quickly I can check Javascript results... Save the file, open it in the browser, and viola. On the other hand, I feel much more 'safe' writing Java code because the compilers spit out much more useful information. Debugging in Javascript can come down to using alerts or commenting out blocks of code to isolate the error.
- Java is more structured and I think it'll be a lot easier to work in big teams and in big codebases as compared to Javascript. Javascript is more expressive, and for smaller teams or singular programs, it allows someone to work much faster.
- Another interesting observation about Javascript when working in the browser is that essentially everything is either a method (function that defines some behavior of an object) or attribute (variable that defines some property of an object). Why is this? Because there is a 'global object' called window that is used as the default context... so when you type code such as:
var a = 5;
it is actually stored onto the window object. You can still access it like
console.log(a);
and it will output 5, but you can also access it like:
console.log(window.a);