Monday, September 7, 2015
JS Random Tips
a) The first thing that you should understand is the scope of the variables are different in JS.
http://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript.
There are no block scope variable in JS. (But in new JS specs, they introduced "let" to implement this)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var
b) Make sure you understand the concept of JS Hoisting. This feature may sound extremely strange for Java developers.
http://www.w3schools.com/js/js_hoisting.asp
c) Make sure you understand the concept of closures.
A closure is an inner function that has access to the outer (enclosing) function’s variables—scope chain. This is a crucial concept, as it allows simulation of private variables. It also forces folks to think in terms of functional programming which is extremely powerful, but it gets time to understand it.
http://javascriptissexy.com/understand-javascript-closures-with-ease/
d) Make sure you understand IIFE . This is a very powerful pattern to make sure that your variables are not creeping into global scope.
http://gregfranko.com/blog/i-love-my-iife/
http://javascriptissexy.com/12-simple-yet-powerful-javascript-tips/
e) Use Object.freeze to create immutable object. It ensures that your object can not be modified/tampered.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze
f) Other small tips:
1) use === instead of =
2) If you forget var inside a function, then that variable will become global.
3) JS will automatically insert ; Hence { is recommended to be used at right of function.
4) Arrays use string keys.. Hence splice may not work.
g) Learn prototypes as thats a way to extend objects
http://javascriptissexy.com/javascript-prototype-in-plain-detailed-language/
h) Learn Monads, a way to chain composition
http://sean.voisen.org/blog/2013/10/intro-monads-maybe/
Subscribe to:
Posts (Atom)