Sunday, October 2, 2011

Learning JavaScript

Though a little late, I finally realized that I would be pretty soon ostracized if I don't learn JavaScript. Being in an Internet company, it's more of a mandate rather than a nice to know language. Given that, I started learning it right today & thought it's a good thing to capture some of the important points for later reference.
  1. JS doesn't have the notion of Classes. Everything is an Object. They call it the 'prototypal OO' language to mark this distinction. To remember this fact, here is an example. In classic OO language, we would say "create an object called Bob which is of class Person" whereas in JS, we've to say, "Take the object Person & reuse it as a prototype for a new object called Bob".
  2. In JS, there are no private/public/protected access modifiers. Everything is public. But the effect of the same is achieved using scope.
  3. Nice things like 'Infinity', 'NaN' and '2e5' are available to express the numbers very clearly.
  4. All functions have the hidden argument called 'arguments' that helps in accessing all the parameters passed to that function as an array.
  5. Variables not declared using 'var' are implicitly declared with global scope.
  6. In JS, functions are data. That is, a function with name 'foo' is actually a variable of type 'function' with the value as the body of the function.
  7. 'Scope Chaining' exists in JS. That is, an inner function can access variables defined in the scope of the outer function. Also, it follows the usual 'Lexical Scope' for functions. That is, the environment is created while the function is defined rather than when it is run.
  8. A variable can be undefined using the 'delete <variable>' syntax.
  9. 'Closure' is a concept where a function keeps a link to its parent's scope even after the parent has returned. There are various ways in which this can happen & should be handled very carefully. The access protection mentioned in point 2 can be achieved using this.

No comments:

Post a Comment