escope is a neat project extracting scopes from JS: https://github.com/estools/escope (hl-sexp-mode is great for lisp, similar tools would help JS!)