Hacker News new | ask | show | jobs
by z3t4 3317 days ago
embrace higher-order functions. Use named functions and closures!

example code:

  var anchors = document.getElementsByTagName(“a”);
  for(var i=0,len=anchors.length; i<len; i++){
    alertClickAnchor(i); // Named function
  }

  function alertClickAnchor(i) { // Closure
    anchors[i].onclick = function() {
      alert(i);
    }
  }
1 comments

This makes anything but basic life cycles very hard to read.
You can look at each function at a time and only have to understand what that function does. No more nesting.